### Expected expression for else

I get "Expected expression" errors for else. How do I fix this?

 ``123456789101112131415161718192021222324252627282930313233343536`` ``````#include #include using namespace std; int main() { float num[3]; string operation; cout << "Input your first number: " << "\n"; cin >> num[0]; cout << "Input your second number: " << "\n"; cin >> num[1]; cout << "Choose your operation: (*, /, +, -)" << "\n"; cin >> operation; if (operation=="*") num[2] = num[0] * num[1]; cout << "The answer is: " << num[0] << " X " << " = " << num[1]; else if (operation=="/") num[2] = num[0] / num[1]; cout << "The answer is: " << num[0] << " ÷ " << " = " << num[1]; else if (operation=="+") num[2] = num[0] + num[1]; cout << "The answer is: " << num[0] << " +" << " = " << num[1]; else if (operation=="-") num[2] = num[0] - num[1]; cout << "The answer is: " << num[0] << " - " << " = " << num[1]; return 0; }``````
Why dont you use "switch" instead of if?!? :)
Last edited on
Without curly braces "if" executes only on statement
 ``123`` `````` if (operation=="*") num[2] = num[0] * num[1]; //this statement is executed only if condition is true cout << "The answer is: " << num[0] << " X " << " = " << num[1]; // this one is always executed ``````

This will work (don't forget to do the same for all other 'else if' statements)
 ``123456`` ``````if (operation=="*") { num[2] = num[0] * num[1]; cout << "The answer is: " << num[0] << " X " << " = " << num[1]; }``````

Last edited on
closed account (Dy7SLyTq)
a) switch is not guaranteed to have a performance boost over if/else if/else
b) there is nothing wrong with if/else if/else
c) switch cant take strings without a hack
d) as to your problem op, we aren't writing python code. unless you only have one statement directly after the if/else if/else/while/do-while/for, then you need {} around the statements
 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include #include using namespace std; int main() { float num[3]; string operation; cout << "Input your first number: " << "\n"; cin >> num[0]; cout << "Input your second number: " << "\n"; cin >> num[1]; cout << "Choose your operation: (*, /, +, -)" << "\n"; cin >> operation; if (operation=="*"){ num[2] = num[0] * num[1]; cout << "The answer is: " << num[0] << " X " << " = " << num[1]; } else if (operation=="/"){ num[2] = num[0] / num[1]; cout << "The answer is: " << num[0] << " ÷ " << " = " << num[1]; } else if (operation=="+"){ num[2] = num[0] + num[1]; cout << "The answer is: " << num[0] << " +" << " = " << num[1]; } else if (operation=="-"){ num[2] = num[0] - num[1]; cout << "The answer is: " << num[0] << " - " << " = " << num[1]; } return 0; }``````

This is Why it doesnt work! You must use { at start and } at the end of each else if! Because you want program to do some commands for each condition!
@Tekonos

One other small problem with your program. You're getting the results of the operations, but not displaying it.

 ``123456`` `````` if (operation=="*") { num[2] = num[0] * num[1]; cout << "The answer is: " << num[0] << " X " << num[1] << " = " << num[2] << endl; } ....``````

etc., for each of the other 3, as well.
Last edited on
Thanks, I got it fixed, but could there be a better alternative in writing this code?

 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````#include #include using namespace std; int main() { float num[3]; string operation; cout << "Input your first number: " << "\n"; cin >> num[0]; cout << "Input your second number: " << "\n"; cin >> num[1]; cout << "Choose your operation: (*, /, +, -)" << "\n"; cin >> operation; if (operation=="*"){ num[2] = num[0] * num[1]; cout << "The answer is: " << num[0] << " X " << num[1] << " = " << num[2] << endl; } else if (operation=="/"){ num[2] = num[0] / num[1]; cout << "The answer is: " << num[0] << " ÷ " << num[1] << " = " << num[2] << endl; } else if (operation=="+"){ num[2] = num[0] + num[1]; cout << "The answer is: " << num[0] << " +" << num[1] << " = " << num[2] << endl; } else if (operation=="-"){ num[2] = num[0] - num[1]; cout << "The answer is: " << num[0] << " - " << num[1] << " = " << num[2] << endl; } return 0; }``````
Lines 21 26 31 36 are almost identical. You can simplify the code and print the result after you do the math in if-else blocks
 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````#include #include using namespace std; int main() { float num[3]; string operation; cout << "Input your first number: " << "\n"; cin >> num[0]; cout << "Input your second number: " << "\n"; cin >> num[1]; cout << "Choose your operation: (*, /, +, -)" << "\n"; cin >> operation; if (operation=="*") num[2] = num[0] * num[1]; else if (operation=="/") num[2] = num[0] / num[1]; else if (operation=="+") num[2] = num[0] + num[1]; else if (operation=="-") num[2] = num[0] - num[1]; cout << "The answer is: " << num[0] <
Last edited on
closed account (iAk3T05o)
I think so. Started learning arrays today but have made this type of calculator in less lines (not using arrays)
Topic archived. No new replies allowed.