Another simple code problem.

Hello again, another example from my book, this time it's a very simple calculator, on page 103/1100. Cannot seem to figure out why this if statement is not working.

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 "StdAfx.h"
#include "../std_lib_facilities.h"

int main()
{
	char op;
	double first_number;
	double second_number;
	cout << "Please enter the operator you wish to use: \n";
	cin >> op;

	if (op=='+')
		cout << "Please enter the two numbers you wish to add: \n";
	cin >> first_number >> second_number;
	cout << first_number << " + " << second_number << " = " << first_number+second_number << endl;
	
	else if (op=='-')
		cout << "Please enter the two numbers you wish to subtract: \n";
	cin >> first_number >> second_number;
	cout << first_number << " - " << second_number << " = " << first_number-second_number << endl;
	
	else if (op=='/')
		cout << "Please enter the two numbers you wish to divide: \n";
	cin >> first_number >> second_number;
	cout << first_number << " / " << second_number << " = " << first_number/second_number << endl;
	
	else if (op=='*')
		cout << "Please enter the two numbers you wish to multiple; \n";
	cin >> first_number >> second_number;
	cout << first_number << " * " << second_number << " = " << first_number*second_number << endl;

	else
		cout << "Please enter one of the available operators\n";

		
		
keep_window_open();
return 0;
}


Error: error C2181: illegal else without matching if

Probably something silly, just cannot seem to find the solution.
The indentation shows the problem pretty clearly.

1
2
3
4
5
6
	if (op=='+')
		cout << "Please enter the two numbers you wish to add: \n"; // <- this is part of the if block
	cin >> first_number >> second_number;  // <-  this is not
	cout << first_number << " + " << second_number << " = " << first_number+second_number << endl; // <- neither is this
	
	else if (op=='-')  // <- this was not preceeded by an if block 


Remember that an if block stops at the first semicolon. If you want a larger if block, you have to put the code in braces.

1
2
3
4
5
6
7
8
	if (op=='+')
	{  // <- put it in braces
		cout << "Please enter the two numbers you wish to add: \n";  // <- now all of this is part of the if block
		cin >> first_number >> second_number;
		cout << first_number << " + " << second_number << " = " << first_number+second_number << endl;
	}
	
	else if (op=='-')  // <- now this will work, because it's immediately preceeded by an if block. 
Last edited on
Tyvm!
You could substitute if-else statements for the switch statement.

For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch ( op )
{
case '+':
	cout << "Please enter the two numbers you wish to add: \n";
	cin >> first_number >> second_number;
	cout << first_number << " + " << second_number << " = " << first_number+second_number << endl;
	break;
case '-':
	// some code
	break;
// ...
default:
	cout << "Please enter one of the available operators\n";
	break;
}
Topic archived. No new replies allowed.