This is a simple program with a select statement. The problem is when the user enters a char when an int is expected the program goes into an infinite loop. I have tried a few ways to validate the user is entering an integer, but I have been unsuccessful. Does anyone have any ideas?
#include <iostream>
usingnamespace std;
//Function Prototype
void displayMenu();
int main()
{
// Constants for menu choices
constint ENTER_RECORD = 1,
DISPLAY_RECORD = 2,
DELETE_RECORD = 3,
CHANGE_RECORD = 4,
DISPLAY_ALL = 5;
int userChoice = 6;
do{
displayMenu();
//Get the users choice
do
{
cout << "Enter Your Choice (1-6): ";
cin >> userChoice;
}
while (userChoice < 1 || userChoice > 6);
//Process userChoice
switch(userChoice)
{
//*******************Enter Records to File*******************
case ENTER_RECORD:
cout << "\nYou selected Enter a new Customer Account.\n\n";
break;
//******************Display a Particular Account*************
case DISPLAY_RECORD:
cout << "\nYou selected Display a Customer Account.\n\n";
break;
//******************Delete a Particular Account***************
case DELETE_RECORD:
cout << "\nYou selected Delete a Customer Account.\n\n";
break;
//******************Change a Particular Account***************
case CHANGE_RECORD:
cout << "\nYou selected Change a Customer Accounts.\n\n";
break;
//******************Display All Accounts**********************
case DISPLAY_ALL:
cout << "\nYou selected Display All Accounts.\n\n";
break;
//*****************Anything not between 1-5********************
default:
break;
}//End of switch statement
}
while (userChoice != 6);
return 0;
}
////////////////////Function to Display Menu///////////////////////
void displayMenu()
{
cout << "\n * * * * A C C O U N T M E N U * * * * \n\n";
cout << "1. Enter a New Customer Account\n";
cout << "2. Display a Customer Account\n";
cout << "3. Delete a Customer Account \n";
cout << "4. Change a Customer Account\n";
cout << "5. Display All Accounts\n";
cout << "6. Exit the Program\n\n";
}
////////////////////////////////////////////////////////////////////