【发布时间】:2014-01-26 03:42:57
【问题描述】:
我正在尝试用 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循环时,它永远不会出来,它只是不断要求用户输入一个数字。我已经经历了几次,我不知道问题是什么。有人可以帮忙吗?
【问题讨论】:
-
这个表达式
achecker = achecker - achecker + 1;将1分配给achecker,不管它以前的值是什么(提示:achecker - achecker是零;零加一是一)。 -
.. 并缩进代码
-
while(math=1)看起来不太乐观。尝试while((cin >> math) && (math != 5))并丢失其正上方的cin >> math。 -
你也错过了最终的
}
标签: c++ calculator do-while