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

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````// Calculator Sim // 17 February 2013 // // Chapter 6 // Assignment 4, Part 1 #include 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.