Else before if error

Hello, I just started learning C++ two days ago and I have done some stuff. Am trying to make a basic calculator but I keep getting the error 'else' without a previous 'if' I do not see why it has not worked. I have already searched the internet for this problem but all include semicolon and else if problems. I do not see any problems with my semicolons nor else if.

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
  #include <iostream>

using namespace std;

int main()

{
cout << "Please choose what you want to do." << endl;
cout << "1- Division" << endl;
cout << "2- Multiplication" << endl;
cout << "3- Addition" << endl;
cout << "4- Subtraction" << endl;

int choice;
cin >> choice;

if (choice == 1)
    cout << "Enter a number please" << endl;
    int division1;
    cin >> division1;
    cout << "Enter another number please" << endl;
    int division2;
    cin >> division2;
    int division3 = division1 / division2;
    cout << division3 << endl;
else if (choice == 2)
    cout << "example" << endl;
}
closed account (48T7M4Gy)
You need braces {} enclosing lines 18 to 25
Last edited on
@OP @Kemort

Kemort is right but let me sum it up a bit more for you, there needs to be braces around all if or else if or else statements, so don't forget to add those in.
Last edited on
Thank you for this quick response. Your solution did work, thank you very much.
I will try not to forgot the braces between if and else statements. Thank you both.
closed account (48T7M4Gy)
Line 26 should also start 'else' and not 'else if'

Only one set of braces are necessary. Their purpose is to enclose a block of statements. A single line statement 'block' does not require braces.
Topic archived. No new replies allowed.