Need help with (while loop)

I'm starting to learn about while loop, and still kinda confused how to use it. Could you guys help me update with this code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// 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>
using namespace std;

// Constants for membership rates
const double ADULT = 40.0,
            SENIOR = 30.0,
            CHILD = 20.0;

// Constants for menu choices
const int 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;
}
I don't know why I have the feeling that this is a homework, perhaps this is because of the "instructions."
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?
Thanks alot, shamieh n everyone. Thanks to your explanation, I understand how to use "while loop" now :).
Topic archived. No new replies allowed.