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: 3766166 • Letter: W

Question

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 units currently in the machine The program should create an array of five structures. The elements should be initialized with the following data:

Coke (can) 0.75, 10, Coke (bottle) 1.25, 10 ,Mountain Dew (can), 0.75, 10, Water ,1.25 ,10, Full Throttle ,2.00, 10

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 (that has not sold out), the program should display the cost of the drink and subtract one from the number of units 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.

Use named constants for the number of drinks and the initial quantity. • The drinks array must be processed using loops. (Do not use literals for subscripts: drinks[0], drinks[1], etc., use a variable instead: drinks[j]). • Use functions as we will discuss in class (or as you see fit) (or use no functions for partial credit). Since the array is always full (not a partially filled array) you do not need to pass the size as a parameter to the functions (but you can if you want to). For full credit, you need at least three functions that have an array parameter. • I recommend using integers for the drink choices (like a menu). However: • Do not use a switch to process the drink choices. If the choice is any one of the drinks (1-5) you will process them using the same code. • Validate the drink choice input from the user (1-6). • Calculate the earnings by iterating over the array AFTER the user has chosen to quit the program (do not compute the earnings inside the main loop).

Here's what I have so far:

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

struct Machine
{
string Drink_name;
float cost;
int num_drinks;
};

struct Machine drink[] = {
{"Cola (can)", .75, 10},
{"Cola(bottle)", 1.25, 10},
{ "Mountain Dew(can)", .75, 10},
{"Water", 1.25, 10},
{"Full Throttle", 2.00, 10}};

int main()
{
int menu_choice;
float total = 0;

while (menu_choice != 6)

{

for(int i=0; i <5; i++)
{
cout << i + 1 <<". " << drink[i].Drink_name << endl;
}
cout <<"Choose your drink: ";

cin >> menu_choice;

if (menu_choice == 6)
{
cout << " You have earned $" << total << endl;
return 0;
}
if (menu_choice >= 1 && menu_choice <= 5)
{
menu_choice = menu_choice -1;
cout << " This drink costs $" <<
fixed << setprecision(2) << drink[menu_choice].cost << " ";
total = total + drink[menu_choice].cost;
drink[menu_choice].num_drinks = drink[menu_choice].num_drinks - 1;
if (drink[menu_choice].num_drinks <= 0)
cout << drink[menu_choice].Drink_name << " is sold out" << endl;
}
}
return 0;

}

I just need to add in functions and named constants and I am lost.

The sample output looks like this:

Explanation / Answer

Complete Program:

#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;

struct Machine
{
string Drink_name;
float cost;
int num_drinks;
};

const int MAX_DRINKS = 5;

int readUserChoice(Machine drink[])
{
int menu_choice = 0;

for(int i = 0; i < MAX_DRINKS; i++)
{
  cout << i + 1 <<". " << drink[i].Drink_name << endl;
}
cout << "6. Quit the Program" << endl;
cout << " Enter your choice: ";
cin >> menu_choice;

return menu_choice;
}

void updateMachine(Machine drink[], int menu_choice, float &total)
{
menu_choice = menu_choice - 1;

if (drink[menu_choice].num_drinks <= 0)
  cout << drink[menu_choice].Drink_name << " is sold out" << endl;
else
{
  cout << "The cost is: $" << fixed << setprecision(2) << drink[menu_choice].cost << " ";
  total = total + drink[menu_choice].cost;
  drink[menu_choice].num_drinks = drink[menu_choice].num_drinks - 1;
}

cout << endl;
}

int main()
{
struct Machine drink[] = {{"Coke (can)", .75, 3},
       {"Coke (bottle)", 1.25, 10},
       {"Mountain Dew (can)", 0.75, 10},
       {"Water", 1.25, 10},
       {"Full Throttle", 2.00, 10}};

int menu_choice = 0;
float total = 0;

while (menu_choice != 6)
{
  menu_choice = readUserChoice(drink);


  if(menu_choice >= 1 && menu_choice <= 5)
  {
   updateMachine(drink, menu_choice, total);
  }
  else if(menu_choice == 6)
  {
   cout << "Amount earned: $" << fixed << setprecision(2) << total << endl;
  }
  else
  {
   cout << "Invalid choice ";
  }  
}

cout << endl;
system("pause");
return 0;
}

Sample Output:

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