#include <iostream>
using namespace std;
int main()
{
int num1,num2,answer;
char choice = 'Y',input;
while (choice == 'Y' || choice == 'y')
{
cout << "Enter the first number: " << endl;
cin >> num1;
cout << "Enter the second number: " << endl;
cin >> num2;
cout << "What operation would you like to use?" << endl << endl;
cout << "Press [A] if you want to use Addition." << endl;
cout << "Press [S] if you want to use Subtraction." << endl;
cout << "Press [M] if you want to use Multiplication." << endl;
cout << "Press [D] if you want to use Division." << endl;
switch(input)
{
case 'A':
case 'a':
{
answer=num1+num2;
cout << "This is the sum of your equation: " << answer << endl;
break;
}
case 'S':
case 's':
{
answer=num1-num2;
cout << "This is the difference of your equation: " << answer << endl;
break;
}
case 'M':
case 'm':
{
answer=num1*num2;
cout << "This is the product of your equation: " << answer << endl;
break;
}
case 'D':
case 'd':
{
answer=num1/num2;
cout << "This is the quotient of your equation: " << answer << endl;
break;
}
default:
{
cout << "Invalid Operation..." << endl;
break;
}
}
cout << "Do you want to go again? (Y/N) " << endl;
cin >> choice;
}
cout << "See you later." << endl;
return 0;
}
所以我大约一个半月前刚刚开始上大学,我想我应该尝试一下还没有教给我们的代码。但我遇到了一个问题,每当我构建程序时,它都没有显示错误。但它并没有达到我的预期,即成为一个计算器。它立即跳到:“你想再去一次吗?”输入这两个数字后,它甚至不让用户选择操作,更不用说计算它了。我的代码似乎有什么问题?
[编辑] 我忘了输入 cin >> 输入;在询问要使用哪个操作之后。
最佳答案
正如评论所建议的,您需要在某个时刻获取 input
变量的值。我建议在依赖于它的switch
之前:
cin >> input; // You forgot to put this line in, I think!
switch(input)
{
...
关于c++ - 我的 C++ 计算器似乎出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58304297/