P 667 13. Drink Machine Simulator Write a program that simulates a soft drink ma
ID: 3633887 • Letter: P
Question
P 667
13. Drink Machine Simulator
Write a program that simulates a soft drink machine. The program should use a
structure that stores the following data:
Drink Name
Drink Cost
Number of Drinks in Machine
The program should create an array of five structures. The elements should be
initialized with the following data:
Drink Name Cost Number in Machine
-------------------------------------
Cola .75 20
Root Beer .75 20
Lemon-Lime .75 20
Grape Soda .80 20
Cream Soda .80 20
Each 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
either quit the program or pick a 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
has sold out, a message should be displayed. The loop then repeats. 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.
****************************************************************************************************
Explanation / Answer
please rate - thanks
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct Machine
{
string name;
double cost;
int num;
};
void init(Machine&,string , double, int);
int menu(Machine[]);
void payment(double);
int main()
{Machine drink[5];
int choice;
double made=0;
init(drink[0],"Cola",.75,20);
init(drink[1],"Root Beer",.75,20);
init (drink[2],"Lemon Lime",.75,20);
init (drink[3],"Grade Soda",.80,20);
init (drink[4],"Cream Soda",.80,20);
choice=menu(drink);
while(choice!=5)
{
payment(drink[choice].cost);
made+=drink[choice].cost;
drink[choice].num--;
choice=menu(drink);
}
cout<<"Today the machine has made $"<<setprecision(2)<<fixed<<made<<endl;
system("pause");
return 0;
}
void payment(double p)
{double pay;
cout<<"Your drink costs $"<<setprecision(2)<<fixed<<p<<endl;
cout<<"Enter payment: ";
cin>>pay;
while(pay<0||pay>1.||pay<p)
{cout<<"please insert the correct amount for your drink! ";
cout<<"maximum payment is $1.00 ";
cout<<"Enter payment: ";
cin>>pay;
}
cout<<"Your change is: $"<<setprecision(2)<<fixed<<pay-p<<endl;
return;
}
void init(Machine& d,string n, double c, int num)
{d.name=n;
d.cost=c;
d.num=num;
}
int menu(Machine d[])
{ int choice=8,i;
bool soldout=true;
while((choice<1||choice>6)||soldout)
{soldout=false;
cout<<"Menu ";
cout<<" Drink Cost left ";
for(i=0;i<5;i++)
{cout<<i+1<<". "<<setw(15)<<left<<d[i].name<<setw(5);
cout<<setprecision(2)<<fixed<<d[i].cost<<" "<<d[i].num<<endl;
}
cout<<"6. Exit ";
cout<<"Enter Choice ";
cin>>choice;
if(choice<1||choice>6)
cout<<"invalid entry ";
else
if(d[choice-1].num==0)
{cout<<"sold out ";
soldout=true;
}
}
return choice-1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.