Program 3. Write a program that displays the various drinks your vending machine
ID: 3889498 • Letter: P
Question
Program 3. Write a program that displays the various drinks your vending machine offers along with the option number for each drink. Beside each option include the price (you can set the cost for the small, the costs for medium should be 2 times the cost of the small and the cost of the large should be 3 times the cost of the small). For example: "1. Tropical Iced Tea (Small $0.75, Medium $1.50, Large $2.25" Then prompt the user for their drink selection and enter their selection. Record the cost of the small drink into a variable that will be used to compute the amount due (to be used in program 4). Display a message such as "You selected Tropical Iced Tea". If the user does not enter a valid option, print an error message and exit the program. You should use a switch statement for this problem. To check that you have recorded the amount due properly, after the switch statement, display the amount due for the small size of the drink selected. You must offer at least 4 drink options.Explanation / Answer
#include<iostream>
int main()
{
int ch, s;
float price, payment;
float d1_small=0.75, d1_medium=1.50,d1_large=2.25;
float d2_small=1.00, d2_medium=1.50,d2_large=2.00;
float d3_small=1.75, d3_medium=2.50,d3_large=3.25;
cout <<"Welcome to New vending machine. Please select your drink: "<<endl;
cout <<"1. Tropical Iced Tea (Small $"<<d1_small<<", Medium $"<<d1_medium<<", Large $"<<d1_large<<endl;
cout <<"2. Chilled Coke (Small $"<<d2_small<<", Medium $"<<d2_medium<<", Large $"<<d2_large<<endl;
cout <<"3. Coffee (Small $"<<d3_small<<", Medium $"<<d3_medium<<", Large $"<<d3_large<<endl;
cin >> ch;
switch (ch)
{
case 1:
price=d1_small;
cout <<"Select size: 1 smal, 2 medium, 3 large";
cin >> s;
cout <<"You selected Tropical Iced Tea";
switch (s)
{
case 1:
price=d1_small;
cout <<" Size small"<<endl;
break;
case 2:
price=d1_medium;
cout <<" Size medium"<<endl;
break;
case 3:
price=d1_large;
cout <<" Size large"<<endl;
break;
default:
cout <<"Invalid selection Small selected";
}
break;
case 2:
price=d2_small;
cout <<"Select size: 1 small, 2 medium, 3 large";
cin >> s;
cout <<"You selected Chilled Coke";
switch (s)
{
case 1:
price=d2_small;
cout <<" Size small"<<endl;
break;
case 2:
price=d2_medium;
cout <<" Size medium"<<endl;
break;
case 3:
price=d2_large;
cout <<" Size large"<<endl;
break;
default:
cout <<"Invalid selection Small selected";
}
break;
case 3:
price=d3_small;
cout <<"Select size: 1 smal, 2 medium, 3 large";
cin >> s;
cout <<"You selected Cold Coffee";
switch (s)
{
case 1:
price=d3_small;
cout <<" Size small"<<endl;
break;
case 2:
price=d3_medium;
cout <<" Size medium"<<endl;
break;
case 3:
price=d3_large;
cout <<" Size large"<<endl;
break;
default:
cout <<"Invalid selection Small selected";
}
break;
}
cout <<"Total amount $"<<price<<". Enter your payment"<<endl;
cin >> payment;
cout << "Please take the change $"<<payment-price;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.