我想在C++中做一个简单的计算器。下面是部分代码:程序不会退出执行循环
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
int math;
int a1;
int a2 = 0;
int a3 = 1;
int answer;
int amount = 0;
int achecker = 1;
cout << "Welcome to my calculator! Type '1' to add, type '2' to subtract, "
"type '3' to multiply, type '4' to divide, and type '5' to exit."
<< endl;
cin >> math;
while (math = 1)
{
cout << "Input how many numbers you wish to add:" << endl;
cin >> amount;
achecker = amount;
do
{
cout << "Input the number you wish to add:" << endl;
cin >> a1;
answer = a1 + a2;
a2 = a1;
achecker = achecker - achecker + 1;
} while (achecker < amount);
cout << answer;
}
我遇到的问题是,当程序进入了do-while循环,它永远不会出来,它只是不断要求用户输入一个数。我经历了这几次,我不知道问题是什么。有人可以帮忙吗?
[调高你的警告级别(http://coliru.stacked-crooked.com/a/fbe8856d94bfe1c6) – chris
这个表达式'achecker = achecker - achecker + 1;'将'1'赋予'achecker',而不管它以前的值如何(提示:'achecker - achecker'为零;零加1就是一个)。 – dasblinkenlight
..并缩进代码 –