Write a program that simulates a soft drink machine. The program should use a st
ID: 3766546 • 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. I need to use named constants using loops and also functions. Here's what I have:
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. I need to use named constants using loops and also functions. Here's what I have:
#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;
}
return 0:
}
sample output:
Explanation / Answer
Here is the code for you. If you need any further modifications, just get back to me.
#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 menu()
{
int menu_choice;
for(int i = 0; i < 5; i++)
{
cout << i + 1 <<". " << drink[i].Drink_name << endl;
}
cout<<"6. Quit the Program"<<endl<<endl;
cout <<"Enter your choice: ";
cin >> menu_choice;
return menu_choice;
}
float showAndUpdate(int menu_choice)
{
cout<<"The cost is: $"<<fixed<<setprecision(2)<<drink[menu_choice-1].cost<<endl;
drink[menu_choice-1].num_drinks--;
return drink[menu_choice-1].cost;
}
int main()
{
int menu_choice = 0;
float total = 0;
while (menu_choice != 6)
{
menu_choice = menu();
if (menu_choice == 6)
{
cout << " You have earned $" << total << endl;
return 0;
}
else
{
total += showAndUpdate(menu_choice);
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.