Hot Drink Menu

Stuck in an endless loop, It builds perfectly and I cannot pinpoint what is wrong.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
  // This program uses a do-while loop in a menu.
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
   // Constants for menu choices
   const int COFFEE_CHOICE = 'A',
             TEA_CHOICE = 'B',
             HOT_CHOCOLATE_CHOICE = 'C',
             CAPPUCCINO_CHOICE = 'D',
             EXIT_CHOICE = 'E';

   // Constants drink prices
   const double COFFEE = 1.00,
                TEA = 0.75,
                HOT_CHOCOLATE = 1.25,
                CAPPUCCINO= 2.50;

   // Variables
   int choice;       // Menu choice
   int number;       // Number of cups
   double charges;   // Monthly charges

   // Output formatting.
   cout << fixed << showpoint << setprecision(2);

   do
   {
      // Display the drink menu.
      cout << "\n\t\tHot Beverage Menu\n\n"
           << "A. Coffee           $1.00\n"
           << "B. Tea              $ .75\n"
           << "C. C. Hot Chocolate $1.25\n"
           << "D. Cappuccino       $2.50\n"
           << "E. Exit\n\n"
           << "Please enter your choice: ";
      cin >> choice;

      // Validate the menu selection.
      while (choice < COFFEE_CHOICE || choice > EXIT_CHOICE)
      {
         cout << "Please enter a valid selection: ";
         cin >> choice;
      }

      // Validate and process the user's choice.
      if (choice != EXIT_CHOICE)
      {
         // Get the number of cups.
         cout << "How many cups would you like? ";
         cin >> number;

         // Respond to menu selection.
         switch (choice)
         {
            case COFFEE_CHOICE:
                charges = number * COFFEE;
                break;
            case TEA_CHOICE:
                charges = number * TEA;
                break;
            case HOT_CHOCOLATE_CHOICE:
                charges = number * HOT_CHOCOLATE;
                break;
            case CAPPUCCINO_CHOICE:
                charges = number * CAPPUCCINO;
         }

         // Display the tolal charges.
         cout << "Your grand total is $" << charges << endl;
      }
   } while (choice != EXIT_CHOICE);
   return 0;
}
My guess would be that it's because you're declaring choice as an int. This means that cin will consider 'E' invalid input for the variable.

Try declaring it as a char. After all, that's what you're using it to store.
In addition to what Mikey said; you should probably add a default case statement.
Last edited on
Thank you two so much for your help. I really appreciate it.
You're welcome! Glad I could help :)
Just going to add an extra cent worth.

It is good practice to declare each variable on it's own line. Your code looks innocent enough, but I have a vague idea that the qualifiers like const only apply to the first variable & not to the rest of them. I don't have a copy of the standard with me at the moment, maybe someone could verify this?

Even if I am wrong, it is still good practice to declare & initialise 1 variable per line.
Topic archived. No new replies allowed.