HELP..
below is part of my code which
i have trouble with it
why when i choose 5 to exit..
it does not exit..but it will ask me to press any key to continue
then when i press...it cum back to the main menu again..
thx for helping...really appreciate it

#include <iostream>
using namespace std;


void PressAnyKeyToContinue(void)
{
     system("pause");   
     system("cls");    
}

int MainMenu(void)
{
int choice=0;
bool gotInput = false; 

cout << "==========================================" << endl;
cout << " Personal Financial Record Keeping System " << endl;
cout << "==========================================" << endl;
cout << "1. Update Income" << endl;
cout << "2. Update Fixed Expenses" << endl;
cout << "3. Update Variable Expenses" << endl;
cout << "4. Calculate Totals" << endl;
cout << "5. Exit" << endl;
cout << "==========================================" << endl;

do 
{
     cout << "Please input your choice:";
     if(!(cin >> choice))                   
     {
              cin.clear();
              cin.ignore(10000,'\n');
              gotInput = true;
     }
     if (choice >= 1 && choice<=9) gotInput = true;
} while(!gotInput);
  
return choice;                                
}

int main()
{
int choice;
bool flag = false;                      

do{
      choice = MainMenu();
      system("cls");
      if (choice == 1) updateIncome();      
      else if (choice == 2) updateFixedExpenses();
      else if (choice == 3) updateVariableExpenses();
      else if (choice == 4) calcTotal();
      else if (choice == 5) flag = true;
      else cout << "Use only number shown in the menu" << endl;  
      if(!flag) PressAnyKeyToContinue();
}while (!flag);

system ("pause");
return 0;    
}

Options:
1)Drop the validation routine until you get the program flow working, then come back and add it later.

If it turns out the validation scheme isn't working, and I think that's the problem, then you'll know where to look and be able to ask a better question.

2) Place a cout << choice; in main() after obtaining value and before going into menu to be sure you get the value you think you have.

3) Use a debugger to track variables to see where things go bad.

This article has been dead for over six months. Start a new discussion instead.