Else + If problems

I am making a basic calculator to help me laern the basics of C++. However, I get the message
1
2
3
4
5
c:\documents and settings\gio\my documents\c++ programs\text games & programs\calculator\calculator\main.cpp(26): error C2181: illegal else without matching if
c:\documents and settings\gio\my documents\c++ programs\text games & programs\calculator\calculator\main.cpp(32): error C2181: illegal else without matching if
c:\documents and settings\gio\my documents\c++ programs\text games & programs\calculator\calculator\main.cpp(38): error C2181: illegal else without matching if
c:\documents and settings\gio\my documents\c++ programs\text games & programs\calculator\calculator\main.cpp(44): error C2181: illegal else without matching if
c:\documents and settings\gio\my documents\c++ programs\text games & programs\calculator\calculator\main.cpp(47): error C2181: illegal else without matching if

Here is the main.cpp 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
41
42
43
44
int main()
{
	using namespace std;
	bool loop;
	loop = true;
	while(loop)
	{
		float x;
		float y;
		cout << "Welcome to the calculator!";
		cout << "Enter 1 to add, \n2 to subtract, \n3 to multiply,\n4 to divide, \nand 0 to quit.";
		int choice;
		cin >> choice;
		if(choice == 1)
			cout << "Enter the first number";
			cin >> x;
			cout << "Enter the second number";
			cin >> y;
			cout << add(x, y);
		else if(choice == 2)
			cout << "Enter the number to be subtracted from";
			cin >> x;
			cout << "Enter the number that will be removed from" << x;
			cin >> y;
			cout << subtract(x, y);
		else if(choice == 3)
			cout << "Enter the first number";
			cin >> x;
			cout << "Enter the second number";
			cin >> y;
			cout << multiply(x, y);
		else if (choice == 4)
			cout << "Enter the number to be divided";
			cin >> x;
			cout << "Enter the number that " << x << " will be divided by";
			cin >> y;
			cout << divide(x, y);
		else if(choice == 0)
			cout << "Goodbye";
			loop = false;
		else
			cout << "That is not a valid choice";
	}
}


I have other files but these work fine. I am not trying to make a GUI calculator (like the one in the "Accessories" section of the Start Menu in Windows", just a simple calculator with text input. I a using Visual C++ 2010 Express for my Compiler/IDE.
If your if/else statements have more than one line you must use { } to bracket the statements:

1
2
3
4
5
6
7
8
if(true) {
    //multi
    //line
    //code
} else {
    //other
    //stuff
}
Usage of 'if' control statement:

1
2
3
4
5
6
7
if (condition_1)
     statement_1;
else if (condition_2)
     statement_2;
//...
else
     statement;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if (condition_1)
{//<- notice the brackets here...
     statement_1_a;
     statement_1_b;
     //...
}//<- and here
else if (condition_2)
{
     statement_2_a;
     statement_2_b;
     //...
}
//...
else
{
     statement_a;
     statement_b;
     //...
}

You might also wanna take a look here -> http://cplusplus.com/doc/tutorial/control/
As firedraco beat me to it, I'd just like to slip in a tiny comment that else if is perfectly valid, just so you know.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if(true) {
    //multi
    //line
    //code
} else if (true) {
    //other
    //stuff
} else if (true) {
    //then
    //more
    //cpp
} else {
   //and
   //more
   //data
}



-Albatross
Eventhough other people have already helped you, you could also make a switch case, witch would make it so much easier (in my opinion)...
Making a switch case would create this instead of all the if 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
switch(choice)
{
          case 1:
                   cout << "Enter the first number";
			       cin >> x;
       		       cout << "Enter the second number";
			       cin >> y;
			       cout << add(x, y);
			       break;
          case 2:
            cout << "Enter the number to be subtracted from";
			cin >> x;
			cout << "Enter the number that will be removed from" << x;
			cin >> y;
			cout << subtract(x, y);
			break;
          case 3:
            cout << "Enter the first number";
			cin >> x;
			cout << "Enter the second number";
			cin >> y;
			cout << multiply(x, y);
			break;
		  case 4:	
            cout << "Enter the number to be divided";
			cin >> x;
			cout << "Enter the number that " << x << " will be divided by";
			cin >> y;
			cout << divide(x, y);
            break;
         default:
                 cout << "That is not a valid choice";
}


I haven't put in the "goodbye" part, but you probably get the point.

By the way, what library are you using for the divide(x, y), multiply(x, y) etc?

I'd guess the #include <cmath> but i'm not sure. Knowing would make stuff a lot better :)

Cheers!
Topic archived. No new replies allowed.