// This program displays a menu and asks the user to make a // selection. A do-while loop repeats the program until the // user selects item 4 from the menu. // this program has been modifed to illustrate input error //handling months of membership // and a new option available for the adult membership of a //prepackaged personal training option // at a reduced price. #include #include using namespace std; int main() { // Constants for menu choices const int ADULT_CHOICE = 1, CHILD_CHOICE = 2, SENIOR_CHOICE = 3, QUIT_CHOICE = 4; // Constants for membership rates const double ADULT = 40.0, CHILD = 20.0, SENIOR = 30.0; const double TRAINERRATE = 50.0; //special package rate // Variables int choice; // Menu choice int months; // Number of months double charges; // Monthly charges bool badInput; // used to suppress computing dues if months out of range int trainerhours; // used to pre-buy personal trainer hours if adult // Set up numeric output formatting. cout << fixed << showpoint << setprecision(2); do { badInput = false; // Display the menu. cout << "\n\t\tHealth Club Membership Menu\n\n" << "1. Standard Adult Membership\n" << "2. Child Membership\n" << "3. Senior Citizen Membership\n" << "4. Quit the Program\n\n" << "Enter your choice: "; cin >> choice; // Validate the menu selection. while (choice < ADULT_CHOICE || choice > QUIT_CHOICE) { cout << "Please enter a valid menu choice: "; cin >> choice; } // Validate and process the user's choice. if (choice != QUIT_CHOICE) { // Respond to the user's menu selection. switch (choice) { case ADULT_CHOICE: cout << "For how many months? "; cin >> months; if (months >= 3) { charges = months * ADULT; } else { cout << "Sorry, but you must join for at least 3 months\n"; cout << "Try again \n"; badInput = true; break; } cout << "How many hours of personal training would you like to pre-purchase per month (0 to 6) "; cin >> trainerhours; if (trainerhours < 0 || trainerhours > 6) { cout << "Sorry, that is not a valid choice. Try again\n"; badInput = true; break; } charges = months * ADULT+ trainerhours * TRAINERRATE; break; case CHILD_CHOICE: cout << "For how many months? "; cin >> months; if (months < 3) { cout << "Sorry, but you must join for at least 3 months\n"; cout << "Try again \n"; badInput = true; } else charges = months * CHILD; break; case SENIOR_CHOICE: cout << "For how many months? "; cin >> months; if (months < 6) { cout << "Sorry, but you must join for at least 6 months\n"; cout << "Try again \n"; badInput = true; } charges = months * SENIOR ; break; } // Display the monthly charges. if (!badInput) cout << "The total charges are $" << charges << endl; } } while (choice != QUIT_CHOICE); return 0; }