有人可以向我解释为什么这个do-while循环无法正常工作吗?这个do-while循环为什么不起作用?
该程序请求的表达,则该程序输出的正确答案。显示答案后,程序会询问用户是否要执行另一个计算。但是,即使用户指出他们不想再次执行计算,程序仍会循环。
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main() {
float num_one;
float num_two;
char user_operator_one;
char repeat;
cout << "This is a very simple calculator." << endl;
cout << "Please enter your expression: ";
cin >> num_one >> user_operator_one >> num_two;
cout << "\n\n";
do
{
switch (user_operator_one)
{
case '+':
cout << num_one << "+" << num_two << "=" << num_one + num_two << endl;
break;
case '-':
cout << num_one << "-" << num_two << "=" << num_one - num_two << endl;
break;
case '*':
cout << num_one << "*" << num_two << "=" << num_one * num_two << endl;
break;
case '/':
if (num_two == 0) {
cout << "Answer is undefined" << endl;
}
cout << num_one << "/" << num_two << "=" << num_one/num_two << endl;
break;
default:
cout << user_operator_one << "is an unknown operation." << endl;
break;
}
cout << "Would you like to perform another operation (enter a 1 for yes, 0 for no.)";
cin >> repeat;
} while (repeat == 1);
}
你为什么不要求'y'或'n'而不是'1'或'0'? – Barmar
也比较两条浮法这样'num_two == 0'是不是一个好主意 – Rama
即使你解决重复问题,还有另外一个问题:你问的外循环的表达,所以它将继续给的答案相同表达。 – Barmar