Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that simulates a soft drink machine. The program should use a st

ID: 3625881 • Letter: W

Question

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
.65
20
Root Beer
.70
20
Lemon-Lime
.75
20
Grape Soda
.85
20
Water
.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.

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",.65,20);
init(drink[1],"Root Beer",.70,20);
init (drink[2],"Lemon Lime",.75,20);
init (drink[3],"Grade Soda",.85,20);
init (drink[4],"Water",.90,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;
if(pay<p)
      {cout<<"Insufficient funds ";
      return;
      }
while(pay<0||pay>1.)
       {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;
  
}
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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote