Menu Selections in a command prompt
I'm looking for an alternative solution to this mess:
Code:
int main()
{
int selection;
string input;
bool goodinput = false;
bool programexit = false;
cout << "Welcome to the employee database, what would you like to do?" << endl // prompt
<< "1. Add an employee" << endl << "2. Modify an employee" << endl
<< "3. Exit the program" << endl;
while (programexit == false)
{
goodinput = false;
while (goodinput == false)
{
getline(cin, input); //get input
stringstream(input) >> selection; //extract selection integer
if (selection == 1)
{
goodinput = true;
employees.push_back(create());
}
else if (selection == 2)
{
cout << "Enter the name of the employee you'd like to edit: ";
getline(cin, input);
get_employee(input).edit();
goodinput = true;
}
else if (selection == 3)
{
programexit = true;
goodinput = true;
}
else
{
goodinput = false; // invalid selection - continue loop
cout << "Please enter a valid selection, 1 or 2." << endl;
}
}
}
return 0;
}
This works, but it's very limited. Like when I run the create function or edit function, or anything inside the main loop, I have to re-initialize my programexit bool. I also have to reinitialize my input variable, as well as the goodinput bool. So there is 3 local variables that I'm always going to need, no matter what user interaction function I would run. Is there any relatively simple solution to programming context menus? I know I'll have to manually enter in logic for specific commands, but I'm wondering is there a more object oriented approach to dealing with these context menu's? I'm open to a good tutorial on the subject, or some tips if you have any. Thank you!