C++:Problem8

Problem:
Write a program that displays a hot beverage menu and prompts the user to make a selection. A switch statement determines which item the user chosen. A do-while loop repeats until the user selects item E from the menu.

Objectives:
• How to use switch statements
• do-while loop
• while loop used for validation of user input
• for loop
• How to use type casting

Display:
***Hot Beverage Menu***


A: Coffee $1.00
B: Tea $ .75
C: Hot Chocolate $1.25
D: Cappuccino $2.50
E: Exit the program
Enter the beverage A,B,C, or D you desire
(or E to exit the program): a
How many cups would you like ? 3
Your Total cost is: $3.00


***Hot Beverage Menu***

A: Coffee $1.00
B: Tea $ .75
C: Hot Chocolate $1.25
D: Cappuccino $2.50
E: Exit the program
Enter the beverage A,B,C, or D you desire
(or E to exit the program): e
Please come again
Goodbye

Code:
#include
#include
using namespace std;

int main()
{
//declaration of variables
int numCups;
float cost;
char beverage;
bool validBeverage;

cout<< fixed << showpoint << setprecision(2);

//Using do while loop and switch statment
do
{
//Prompts the user to input
cout<< endl << endl;
cout<< "***Hot Beverage Menu***" << endl << endl;
cout<< "A: Coffee $1.00" << endl;
cout<< "B: Tea $ .75" << endl;
cout<< "C: Hot Chocolate $1.25" << endl;
cout<< "D: Cappuccino $2.50" << endl;
cout<< "E: Exit the program " << endl;

cout<< "Enter the beverage A,B,C, or D you desire" < << " (or E to exit the program): ";
cin>> beverage;


validBeverage = false;

switch(beverage)
{
case 'a':
case 'A': cost = 1.00;
validBeverage = true;
break;
case 'b':
case 'B': cost = .75;
validBeverage = true;
break;
case 'c':
case 'C': cost = 1.25;
validBeverage = true;
break;
case 'd':
case 'D':cost = 2.50;
validBeverage = true;
break;

case 'e':
case 'E': cout<< "Please come again"<< endl;
break;

default : cout<< "That's not on the menu."< }

//Using if statment to display the output
if (validBeverage)
{
cout<< "How many cups would you like ? ";
cin>> numCups;
cout<<"Your Total cost is: $"
<< cost*numCups < }
}while(beverage != 'E' && beverage != 'e');

//closing
cout << "\nEnd of Program\n\n";
return 0;
}