/* testrep12.cpp CIS 150 6/10/2008 David Klick This program demonstrates how repetion can be combined with menus to create a more useful application interface. */ #include #include using std::cin; using std::cout; void cont(); int main() { char choice; // variable for user input bool done = false; // true after user selects exit do { // display menu cout << "\n\nMenu of choices:\n\n" << "1. (F)irst choice\n\n" << "2. (S)econd choice\n\n" << "3. (T)hird choice\n\n" << "4. E(x)it\n\n" << "Please enter choice: "; // get user's menu choice cin >> choice; // decide what to do based on user's choice switch (choice) { case '1': case 'F': case 'f': cout << "You chose the first option!\n"; break; case '2': case 'S': case 's': cout << "You chose the second option!\n"; break; case '3': case 'T': case 't': cout << "You chose the third option!\n"; break; case '4': case 'X': case 'x': done = true; break; default: cout << "Error: Invalid menu choice\n"; } // pause if user didn't choose to exit if (!done) { cout << "Press any key to continue "; cin.ignore(); getchar(); } } while (!done); // repeat until done cout << "\nNormal program termination\n"; cont(); return 0; } void cont() { if (cin.rdbuf()->sungetc() != -1 && cin.get() != '\n') cin.ignore(80,'\n'); cout << "Press enter to continue..."; cin.get(); } /* Sample output: Menu of choices: 1. (F)irst choice 2. (S)econd choice 3. (T)hird choice 4. E(x)it Please enter choice: 0 Error: Invalid menu choice Press any key to continue Please enter choice: 1 You chose the first option! Press any key to continue Please enter choice: 2 You chose the second option! Press any key to continue Please enter choice: 3 You chose the third option! Press any key to continue Please enter choice: 4 Normal program termination */