Expected expression for else

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

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
#include <iostream>
#include <string>
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
1
2
3
   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)
1
2
3
4
5
6
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
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>
#include <string>
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.

1
2
3
4
5
6
 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?

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
38
39
40
#include <iostream>
#include <string>
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
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
38
39
#include <iostream>
#include <string>
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] <<operation << num[1] <<  " = " << num[2] << endl; // notice 'operation'

    
    return 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.