// This program uses a do-while loop in a menu.
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
// Constants for menu choices
constint COFFEE_CHOICE = 'A',
TEA_CHOICE = 'B',
HOT_CHOCOLATE_CHOICE = 'C',
CAPPUCCINO_CHOICE = 'D',
EXIT_CHOICE = 'E';
// Constants drink prices
constdouble 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;
}
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 constonly 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.