Help! My calculator doesn’t work properly!
I keep getting “Invalidate operator. Try again”. What did I do wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <iostream>
using namespace std;
int main(){
char opr;
do{
int a,b,z;
cin >> opr;
if(opr = '+' || '-' || '*' || '/' || '%'){
cin >> a >> b;
switch(opr){
case '+':
z = a + b;
cout << z << endl;
break;
case '-':
z = a - b;
cout << z << endl;
break;
case '*':
z = a * b;
cout << z << endl;
break;
case '/':
z = a / b;
cout << z << endl;
break;
case '%':
z = a % b;
cout << z << endl;
break;
default:
cout << "Invalid operation. Try again." << endl;}
}
}while(opr != 'x' || opr != 'X')
}
|
> if(opr = '+' || '-' || '*' || '/' || '%')
This isn't how you do it.
> while(opr != 'x' || opr != 'X')
This is how you do it.
So use == to compare, and compare each one in turn.
I fixed it just now! Thank you so much!
Topic archived. No new replies allowed.