Problem Statement: Simulate a vending machine! Your vending machine should prese
ID: 3620526 • Letter: P
Question
Problem Statement:Simulate a vending machine!
Your vending machine should present the user with at least 3 products they can
purchase. The user should be able to purchase as many products and as many of each
product as they wish, in any order.
When the user wants to finalize their purchases (check out) they should be prompted
for an amount of money to pay the total bill. If the user needs change, print out the
amount.
Struggling a little on which direction to go with this. Any help would be appreciated.
Explanation / Answer
please rate - thanks this should get you started #include <iostream>#include <iomanip>
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
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.