// UPDATE THIS PROGRAM SO YOU USE A WHILE LOOP FOR EACH INPUT VALUE
// The loop for the choice will loop if the input choice isn't 1-4 inclusive
// The loop for the months will loop if the input months isn't 1-12 inclusive
#include <iostream>
#include <iomanip>
usingnamespace std;
// Constants for membership rates
constdouble ADULT = 40.0,
SENIOR = 30.0,
CHILD = 20.0;
// Constants for menu choices
constint ADULT_CHOICE = 1,
CHILD_CHOICE = 2,
SENIOR_CHOICE = 3,
QUIT_CHOICE = 4;
int main()
{
int choice; // To hold a menu choice
int months; // To hold the number of months
double charges; // To hold the monthly charges
// Display the menu and get a choice.
cout << "\t\tHealth Club Membership Menu\n\n";
cout << "1. Standard Adult Membership\n";
cout << "2. Child Membership\n";
cout << "3. Senior Citizen Membership\n";
cout << "4. Quit the Program\n\n";
cout << "Enter your choice: ";
cin >> choice;
// PUT INPUT VALIDATION for choice while LOOP HERE*****
// Set the numeric ouput formatting.
cout << fixed << showpoint << setprecision(2);
if( choice != QUIT_CHOICE )
{
cout << "For how many months? ";
cin >> months;
// PUT INPUT VALIDATION for months while LOOP HERE*****
// Respond to the user's menu selection.
switch( choice )
{
case ADULT_CHOICE:
charges = months * ADULT;
break;
case CHILD_CHOICE:
charges = months * CHILD;
break;
case SENIOR_CHOICE:
charges = months * SENIOR;
break;
} // end switch
cout << "The total charges are $" << charges << endl;
}
else
{
cout << "You chose to quit the program.\n";
}
return 0;
}
Yes, you could call it a hw/assignment, but more likely an excersise('cause there's no point for doing it). This exercise is for us to try out, but I still don't understand much about "while loop" :/
Well, just think about it. Currently the code is saying
1 2 3 4 5 6
if( choice != QUIT_CHOICE )
{
//Do stuff
}
else
//Get out of there!
Now your teacher wants you to use a while loop correct? Think about it like this. You go to the grocery store and here are the implications: IF you bring an item to the cash register you will pay money and complete a transaction, ELSE if you don't bring an item to the cash register you will NOT pay money and complete a transaction.
Now consider this same situation but in another sense (while).
You go to the grocery store. WHILE you bring an item to the cash register you will pay money and complete a transaction. If you don't have any more items to bring to the cash register then you will leave the store and pay no money at all..
Does this give you an idea on how to write your loop?