/* testmenu1.cpp 6/5/08 David Klick Demonstration of switch statement using a menu example. Includes error hanling on invalid menu selection. */ #include using std::cin; using std::cout; void cont(); int main() { char choice; int numTickets = 0; double price = 0.0; double total = 0.0; bool error = false; bool done = false; cout << "Type of ticket\n\n" << " 1. (C)hild\n\n" << " 2. (A)dult\n\n" << " 3. (S)enior\n\n" << " 4. (Q)uit\n\n" << "Enter choice: "; cin >> choice; switch (choice) { case '1': case 'c': case 'C': price = 40.00; break; case '2': case 'A': case 'a': price = 20.00; break; case '3': case 's': case 'S': price = 45.00; break; case '4': case 'q': case 'Q': done = true; break; default: error = true; cout << "Error: Invalid choice\n\n"; } if (!error && !done) { cout << "\nHow many tickets? "; cin >> numTickets; total = price * numTickets; cout << "The total is $" << total << "\n\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(); }