Calculator problem

Jan 28, 2022 at 3:02pm
Write your question here.
Why isn't my code going through any other of the if else, it only does if then cuts 0ff and gives me the answer for that
[int main()
{
int num1;
char op;
int num2;
cout << "Please enter first number: \n";
cin >> num1;
cout << "Please enter operator: \n";
cin >> op;
cout << "Please enter second number: \n";
cin >> num2;
if(op = '+'){
cout << num1 + num2;
} else if (op = '-'){
cout << num1 - num2;
} else if (op = '*'){
cout << num1 * num2;
} else if(op = '/'){
cout << num1 / num2;
} else{
cout << " \nError, wrong operator or numbers, please enter *, /, -, + thank you \n ";
}
return 0;
}]
Put the code you need help with here.
[
if(op = '+'){
cout << num1 + num2;
} else if (op = '-'){
cout << num1 - num2;
} else if (op = '*'){
cout << num1 * num2;
} else if(op = '/'){
cout << num1 / num2;
} else{]
Jan 28, 2022 at 3:08pm
You are mixing up assignment (=) with a test for equality (==).

e.g.
if(op = '+')
should be
if(op == '+')

Similarly for all the other tests.
Jan 28, 2022 at 3:12pm
thank you, it works now.
Jan 28, 2022 at 3:26pm
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
Topic archived. No new replies allowed.