C++ Language Please Write a program that simulates a soft drink machine. The pro
ID: 3902048 • Letter: C
Question
C++ Language Please
Write a program that simulates a soft drink machine. The program should use a structure named Drink that contains the following information:
? The drink name
? The drink cost
? The number of drinks in the machine
The program should then create an array of 5 Drink structures. The elements should be initialized with the following data:
Drink Name,
Cost,
Number in Machine
Cola
$0.65
20
Root Beer
$0.70
20
Grape Soda
$0.75
20
Lemon-Lime
$0.85
20
Water
$0.90
20
Every time the program runs it should enter a loop that performs the following steps. A list of drinks is displayed on the screen. The user should be allowed to quit the program or select a drink. You can have the user select a drink any way you want. One way would be to have the menu display with a number next to each drink.
If the user selects a drink, he or she will next enter the amount of money that is to be inserted into the drink machine. The program should display the amount of change that would be returned and subtract one from the number of that drink left in the machine. If the user selects a drink that is sold out a message should be displayed. The loop then repeats asking the user if they want to quit or select another drink. When the user chooses to quit the program it should display the total amount of money the machine earned.
Input Validation: When the user enters an amount of money do not accept negative values or values greater than $1.00. When the amount entered is less than the price of the drink display the message “Insufficient Funds” and return to the beginning of the selection loop. Moreover, If the user put 6, the program end with total Revenue and show remain drinks like sample run below
Select a drink <1-5> or enter 6 to quit : 6
Total Revenue : $0.65
Drink Number Left
Cola 19
Root Beer 20
Grape Soda 20
Lemon-lime 20
Water 20
Program ended with exit code: 0
Menu
Drink Cost left
1. Cola 0.65 20
2. Root Beer 0.70 20
3. Lemon Lime 0.75 20
4. Grade Soda 0.85 20
5. Water 0.90 20
6. Exit
Enter Choice 1
Your drink costs $0.65
Enter payment: 2
please insert the correct amount for your drink!
maximum payment is $1.00
Enter payment:
Drink Name,
Cost,
Number in Machine
Cola
$0.65
20
Root Beer
$0.70
20
Grape Soda
$0.75
20
Lemon-Lime
$0.85
20
Water
$0.90
20
Explanation / Answer
#include <iostream>
#include<iomanip>
using namespace std;
struct Drink
{
string name;
double cost;
int number;
};
int main() {
struct Drink drink[5];
int choice;
double payment;
double total = 0;
drink[0] = {"Cola ",0.65,20};
drink[1] = {"Root Beer ",0.7,20};
drink[2] = {"Grape Soda",0.75,20};
drink[3] = {"Lemon-Lime",0.85,20};
drink[4] = {"Water ",0.9,20};
do
{
cout<<" Menu"<<endl;
cout<<"Drink Cost left"<<endl;
for(int i = 0;i<5;i++)
//display structure elements
cout<<(i+1)<<"."<<drink[i].name<<setw(10)<<drink[i].cost<<setw(10)<<drink[i].number<<endl;
cout<<"6. Exit"<<endl;
cout<<"Enter Choice"<<endl;
cin>>choice;
switch(choice)
{
case 1: if(drink[0].number == 0)
cout<<" Sold out ";
else
{
cout<<" Your drink costs $0.65"<<endl;
drink[0].number--;//decrease the number of drinks
cout<<"Balance : $"<<1.00 - drink[0].cost;
total = total + drink[0].cost;// total payment
}
break;
case 2: if(drink[1].number == 0)
cout<<" Sold out ";
else
{
cout<<" Your drink costs $0.700"<<endl;
drink[1].number--;
cout<<"Balance : $"<<1.00 - drink[1].cost;
total = total + drink[1].cost;
}
break;
case 3: if(drink[2].number == 0)
cout<<" Sold out ";
else
{
cout<<" Your drink costs $0.75"<<endl;
drink[2].number--;
cout<<"Balance : $"<<1.00 - drink[2].cost;
total = total + drink[2].cost;
}
break;
case 4: if(drink[3].number == 0)
cout<<" Sold out ";
else
{
cout<<" Your drink costs $0.85"<<endl;
drink[3].number--;
cout<<"Balance : $"<<1.00 - drink[3].cost;
total = total + drink[3].cost;
}
break;
case 5: if(drink[4].number == 0)
cout<<" Sold out ";
else
{
cout<<" Your drink costs $0.9"<<endl;
drink[4].number--;
cout<<"Balance : $"<<1.00 - drink[4].cost;
total = total + drink[4].cost;
}
break;
case 6: cout<<" Program ended with exit code: 0";
exit(0);
}
cout<<" Enter payment: "<<endl;
cin>>payment;
if(payment > 1.00)
{
cout<<"please insert the correct amount for your drink!"<<endl;
cout<<"maximum payment is $1.00"<<endl;
cout<<"Enter payment:"<<endl;
cin>>payment;
}
cout<<"Total Payment : $"<<total;
}while(choice != 6);
return 0;
}
Output:
Menu
Drink Cost left
1.Cola 0.65 20
2.Root Beer 0.7 20
3.Grape Soda 0.75 20
4.Lemon-Lime 0.85 20
5.Water 0.9 20
6. Exit
Enter Choice 1
Your drink costs $0.65
Balance : $0.35
Enter payment:2
please insert the correct amount for your drink!
maximum payment is $1.00
Enter payment:1
Total Payment : $0.65
Menu
Drink Cost left
1.Cola 0.65 19
2.Root Beer 0.7 20
3.Grape Soda 0.75 20
4.Lemon-Lime 0.85 20
5.Water 0.9 20
6. Exit
Enter Choice 2
Your drink costs $0.700
Balance : $0.3
Enter payment:1
Total Payment : $1.35
Menu
Drink Cost left
1.Cola 0.65 19
2.Root Beer 0.7 19
3.Grape Soda 0.75 20
4.Lemon-Lime 0.85 20
5.Water 0.9 20
6. Exit
Enter Choice 6
Program ended with exit code: 0
Do ask if any doubt. Please upvote.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.