I tried to improve upon my calculator design, why isn't this working? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I tried to improve upon my calculator design, why isn't this working?

#include <iostream> using namespace std; int main() { int o; int a; int b; int sum; if(o = 1) { sum= a*b; } if(o = 2){ sum= a+b; } if(o = 3) { sum= a/b; } if(o = 4) { sum= a-b; } cin >> o; cin >> a; cin >> b; cout << sum; return 0; }

10th Nov 2016, 6:25 PM
Tristan
2 Answers
+ 5
The if statements should be placed after the user gives the variables values with the cin statements, and before the cout. It isn't working because the variables in the ifs aren't given values yet, and contain random garbage.
11th Nov 2016, 2:15 AM
Tamra
Tamra - avatar
0
Ah, thanks! I'll try it again.
11th Nov 2016, 3:02 AM
Tristan