我做了这个计算器C++计算器不断循环
#include <iostream>
using namespace std;
int main()
{
float a, b, result;
char operation;
while (1) {
cout << "Enter the calculation you want to perform (e.g: 1 + 2)\n>>";
cin >> a >> operation >> b;
switch(operation)
{
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a/b;
break;
default:
cout << "\nInvalid operation. Program terminated." << endl;
return -1;
}
cout << "= " << result << "\n" << endl;
}
return 0;
}
它应该一直在问同样的问题,如果你输入一些无效的终止。
如果在第一次终止时输入了无效的内容。但是如果你输入一个有效的操作,并且第二次输入一个无效的字符,它会一直输出相同的问题+它在无效字符前处理的答案。
举个例子。你输入了什么? –
这就是它的作用:输入您想要执行的计算(例如:1 + 2) >> 1 + 2 = 3 输入您要执行的计算(例如:1 + 2)012xx>> x = 2 输入要执行的计算(例如:1 + 2)>> = 2 输入要执行(例如:1 + 2)的计算 >> = 2 输入要计算你想要执行(例如:1 + 2) >> = 2等。 – Roemerdt
学习使用调试器是一件好事。 – mathematician1975