error C2181: illegal else without matching if

My assignment is to write a simple calculator. I keep getting
Error 1 error C2181: illegal else without matching if c:\users\sx\dropbox\c++ intro folder\calculatorsim\calculatorsim\blankenshippaulcalculatorsim.cpp 44 1 CalculatorSim

and
Error 2 error C2181: illegal else without matching if c:\users\sx\dropbox\c++ intro folder\calculatorsim\calculatorsim\blankenshippaulcalculatorsim.cpp 48 1 CalculatorSim


I checked for semi-colons after the "if" lines read several forum posts and looked at a couple of books but I don't see where I am going wrong.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Calculator Sim
// 17 February 2013
//
// Chapter 6
// Assignment 4, Part 1

#include <iostream>
using namespace std;

int main()
{
	// Declare variables
	int first = 0;
	int second = 0;
	char mathOp = ' ';
	int result = 0;

	// Enter the first number to be used
	cout << "This program will simulate a calculator using integers and simple math. " << endl ;
	cout << "Please enter the first integer (whole number) to be used in the calculation: " << endl ;
	cin >> first;

	// Enter the operator
	cout << "Please enter the operator sign. Use + for addition, - for subtraction, * for multiplication, or / for division." << endl ;
	cin >> mathOp;

	// Enter the second number to be used
	cout << "Please enter the second integer (whole number) to be used in the calculation: " << endl ;
	cin >> second;

	// Prevent division by zero to avoid ripping the fabric of space and time
	if (second == 0)
		{
			cout << "Error, division by zero not allowed." << endl;
			exit(0);
		}
	// end if

	// Calculation
	// Determine the operation and implement

	// Multiplication
	if (mathOp == '*')
	{result = first * second;}
	// Division
	else if (mathOp == '/')
	{result = first / second;}
	// Addition
	else if (mathOp == '+')
	{result = first + second;}
	// Subtraction
	else if (mathOp == '-')
	{result = first - second;}
	// Reject erroneous input for the operator
	else   // default
	{cout << "Input error for the operator. Please select one of the choices above for the operator next time." << endl;}
	// end if

	// Display result
	cout << result << endl;
}
Last edited on
It compiles and runs fine for me.
Thanks for checking it, Peter. Which compiler did you use? I used MSVS Express 2012.
GCC 4.7.1
Once I actually compiled and ran the program it worked for me. I kept getting those errors while working on the program. MSVS is more complicated than C++.
GNU GCC compiler
Thanks Peter and Greenleaf; I am going to check that out. Things have changed a bit since Fortran 77.
Topic archived. No new replies allowed.