Wojcik_A1.cpp(68): error C2181: illegal else without matching if

I have to build a program which simulates an ATM Machine. I'm pretty sure that I have the code all figured out but I keep getting one error msg illegal else without if.

#include <iostream>
using namespace std;

int main()
{


double savingsmoney = 0;
double checkingmoney = 0;
int xinput_deposit;
int xinput_main;
int xinput_dollar=0;

cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
cout << "Welcome to MyBank ATM" << endl;
cout << "Your Friend in the Market" << endl;
cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
cout << "1 Deposit Money" << endl;
cout << "2 Check the Balance" << endl;
cout << "3 Exit" << endl;
cin >> xinput_main;
while (xinput_main != 3)
{
if (xinput_main == 1)
{
cout << " 1 Deposit into Checking" << endl;
cout << " 2 Deposit into Savings" << endl;
cout << " 3 Back to Main menu" << endl;
cin >> xinput_deposit;
}
while (xinput_deposit != 3)
{
if (xinput_deposit == 1)
{
cout << "Please Enter Dollar Amount = " << endl;
cin >> xinput_dollar;
checkingmoney = checkingmoney + xinput_dollar;
xinput_dollar = 0;
cout << " 1 Deposit into Checking" << endl;
cout << " 2 Deposit into Savings" << endl;
cout << " 3 Back to Main menu" << endl;
cin >> xinput_deposit;
}
else if (xinput_deposit == 2)
{
cout << "Please enter dollar amount = ";
cin >> xinput_dollar;
savingsmoney = savingsmoney + xinput_dollar;
xinput_dollar = 0;
cout << " 1 Deposit into Checking" << endl;
cout << " 2 Deposit into Savings" << endl;
cout << " 3 Back to Main menu" << endl;
cin >> xinput_deposit;
}
else
{
cout << "Invalid Entry!.. Try again" << endl;
cout << "1 Deposit into Checking" << endl;
cout << "2 Deposit into Savings" << endl;
cout << "3 Back to Main Menu" << endl;
}

cout << "1 Deposit Money" << endl;
cout << "2 Check the balance" << endl;
cout << "3 Exit" << endl;
cin >> xinput_deposit;
}
******else if (xinput_main == 2)
{
cout << "Your Checking Balance =" << checkingmoney << endl;
cout << "Your Saving Balance = " << savingsmoney << endl;
cout << "1 Deposit Money" << endl;
cout << "3 Exit" << endl;
cin >> xinput_main;
}
else
{
cout << " Invalid Entry ! Please try again" << endl;
cout << "1 Deposit Money" << endl;
cout << "2 Check the Balance" << endl;
cout << "3 Exit" << endl;
cin >> xinput_main;
}


cout << "====================================" << endl;
cout << "Thank you for your business Today" << endl;
cout << "Hope to see you sometime soon..Bye" <<endl;
}
system ("pause");
return 0;
}


The line with the ***** is where I am getting the error msg. Can anyone help me out????
I have checked your code,there is no previous if before line "****** else if(xinput_main==2);"

At line under "cin>> xinput_deposit;" you closed (first)while loop and you can't use else if without using if first.
My proffessor gave us a hint page where we need to use a while loop within a while loop in order for the program to run correctly.

doesn't the following line of code satisify the If for the else if statement???
if (xinput_main == 1)
{
cout << " 1 Deposit into Checking" << endl;
cout << " 2 Deposit into Savings" << endl;
cout << " 3 Back to Main menu" << endl;
cin >> xinput_deposit;

There is a while loop between if (xinput_main == 1) and "******else if (xinput_main == 2) .That's why they don't match.
Last edited on
Topic archived. No new replies allowed.