我正在尝试用C ++实现一个简单的计算器。但每次我的代码都会给我一个错误,虽然我认为它是正确的,抱歉任何不情愿的错误。我是C ++的新手。
我的代码:
#include<iostream>
using namespace std;
int main()
{
char symbol;
int x,y;
cout<<"What operation you want to do?\n+ - * / : ";
cin>>symbol;
if(symbol != '+' || symbol != '-' || symbol != '*' || symbol != '/')
cout<<"Wrong symbol chosen."<<endl;
else
{
cout<<"Enter two numbers. separate with space : ";
cin>> x >> y;
if(symbol == '+')
cout<<x<<" + "<<y<<" = "<<x+y<<endl;
else if(symbol == '-')
cout<<x<<" - "<<y<<" = "<<x-y<<endl;
else if(symbol == '*')
cout<<x<<" * "<<y<<" = "<<x*y<<endl;
else if(symbol == '/')
cout<<x<<" / "<<y<<" = "<<x/y<<endl;
//}
return 0;
}
每次我运行时,都会放一个符号(+, - ,*或/)&amp;然后它告诉我“选择了错误的符号”。 ,我不明白为什么会这样。我在Windows 10上使用Code :: Blocks v 16。
提前致谢
答案 0 :(得分:3)
你的病情错了!
if(symbol != '+' || symbol != '-' || symbol != '*' || symbol != '/')
cout<<"Wrong symbol chosen."<<endl;
用这个改变上述条件 -
if(symbol != '+' && symbol != '-' && symbol != '*' && symbol != '/')
cout<<"Wrong symbol chosen."<<endl;