Write a program that simulates a soft drink machine. The program should use a st
ID: 3641932 • 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 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
Coca-Cola .75 20
Root Beer .75 20
Sprite .75 20
Spring Water .80 20
Apple Juice .95 20
Please see the input file ("DrinkMachineInventory.txt"). Each time the program runs, it should read the data from the input file and then 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: Only accept positive values for the amount of money. Also, do not accept values greater than $1.00.
Your code should be modular (use functions) and pass variables (by value/by reference) where appropriate. Also, use named constants where appropriate.
Explanation / Answer
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
struct Machine
{
string Drink_name;
float cost;
int num_drinks;
};
struct Machine drink[] = {{"Cola", .75, 20}, {"Root Beer", .75, 20}, { "Orange Soda", .75, 20},
{"Grape Soda", .75, 20}, {"Bottled Water", 1.00, 20}};
int main()
{
int menu_choice;
float moneyin;
float moneyout;
float temp;
while (menu_choice != 6)
{
cout <<"which drink would you like to buy:" << endl;
cout <<"1-Cola" << endl;
cout <<"2-Root Beer" << endl;
cout <<"3-Orange Soda" << endl;
cout <<"4-Grape Soda" << endl;
cout <<"5-Bottled Water" << endl;
cout <<"6-Quit " << endl;
cin >> menu_choice;
if (menu_choice == 1)
{
cout << "enter money to inserted into machine between 0 and $1.00" << endl;
cin >> moneyin;
while (moneyin < drink[0].cost)
{
cout <<"enter" << drink[0].cost << endl;
cin >> moneyout;
temp = 5.00;
if (temp == drink[0].cost)
cout <<"great" << endl;
}
if (moneyin > drink[0].cost)
cout <<" your change is:" << (moneyin - drink[0].cost) << endl;
}
if (menu_choice == 2)
{
cout << "enter money to inserted into machine between 0 and $1.00" << endl;
cin >> moneyin;
while (moneyin < drink[0].cost)
{
cout <<"enter" << drink[0].cost << endl;
cin >> moneyout;
temp = 5.00;
if (temp == drink[0].cost)
cout <<"great" << endl;
}
if (moneyin > drink[0].cost)
cout <<" your change is:" << (moneyin - drink[0].cost) << endl;
}
if (menu_choice == 3)
{
cout << "enter money to inserted into machine between 0 and $1.00" << endl;
cin >> moneyin;
while (moneyin < drink[0].cost)
{
cout <<"enter" << drink[0].cost << endl;
cin >> moneyout;
temp = 5.00;
if (temp == drink[0].cost)
cout <<"great" << endl;
}
if (moneyin > drink[0].cost)
cout <<" your change is:" << (moneyin - drink[0].cost) << endl;
}
if (menu_choice == 4)
{
cout << "enter money to inserted into machine between 0 and $1.00" << endl;
cin >> moneyin;
while (moneyin < drink[0].cost)
{
cout <<"enter" << drink[0].cost << endl;
cin >> moneyout;
temp = 5.00;
if (temp == drink[0].cost)
cout <<"great" << endl;
}
if (moneyin > drink[0].cost)
cout <<" your change is:" << (moneyin - drink[0].cost) << endl;
}
if (menu_choice == 5)
{
cout << "enter money to inserted into machine between 0 and $1.00" << endl;
cin >> moneyin;
while (moneyin < drink[0].cost)
{
cout <<"enter" << drink[0].cost << endl;
cin >> moneyout;
temp = 5.00;
if (temp == drink[0].cost)
cout <<"great" << endl;
}
if (moneyin > drink[0].cost)
cout <<" your change is:" << (moneyin - drink[0].cost) << endl;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.