The program should create an array of five structures. The elements should be in
ID: 3885638 • Letter: T
Question
The program should create an array of five structures. The elements should be initialized with the following data: Drink Name Cola Root Beer Lemon-Lime Grape Soda Cream Soda Cost .75 .75 .75 .80 .80 Number in Machine 20 20 20 20 20 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, 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 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: When the user enters an amount of money, do not accept negative values or values greater than $1.00.Explanation / Answer
PROGRAM CODE:
#include <iostream>
using namespace std;
//structure to store the drinks
struct Drink
{
string drinkName;
double cost;
int numberInMachine;
};
//function to display the drinks
void display(Drink *drinks, int rows)
{
for(int i=0; i<rows; i++)
{
cout<< drinks[i].drinkName<<" $"<< drinks[i].cost <<" "<< drinks[i].numberInMachine<<endl;
}
}
//to search and find out if a drink selected exists or not
int search(Drink *drinks, int rows, string drinkName)
{
cout<<endl;
for(int i=0; i<rows; i++)
{
if(drinks[i].drinkName == drinkName)
return i;
}
return -1;
}
int main() {
//array of drinks
Drink drinks[] = {{"Cola", 0.75, 20},
{"Root Beer", 0.75, 20},
{"Lemon-Lime", 0.75, 20},
{"Grape Soda", 0.80, 20},
{"Cream Soda", 0.80, 20}};
double totalAmountEarned = 0;
//displaying the drink
display(drinks, 5);
while(true)
{
cout<<" Select a drink or enter exit to quit: ";
string selection;
getline(cin,selection);
if(selection == "exit")
{
cout<<" Machine has earned $"<<totalAmountEarned<<endl;
exit(0);
}
else
{
int index = search(drinks, 5, selection);
double amount = 0;
if(index == -1)
{
cout<<" Your selection does not exist in our inventory";
}
else if(drinks[index].numberInMachine == 0)
{
cout<<" We are out of "<< selection<<endl;
}
else
{
while(true)
{
cout<<" Enter amount: ";
cin>>amount;
if(amount<=0 && amount>1)
cout<<" Amount cannot be negative or greater than $1.00";
else
break;
}
//adding the amount to machine's total earnings
totalAmountEarned += drinks[index].cost;
if(amount > drinks[index].cost)
cout<<" Your change is $"<< (amount - drinks[index].cost)<<endl;
drinks[index].numberInMachine--;
display(drinks, 5);
}
}
}
return 0;
}
OUTPUT:
Cola $0.75 20
Root Beer $0.75 20
Lemon-Lime $0.75 20
Grape Soda $0.8 20
Cream Soda $0.8 20
Select a drink or enter exit to quit: Cola
Enter amount: 0.90
Your change is $0.15
Cola $0.75 19
Root Beer $0.75 20
Lemon-Lime $0.75 20
Grape Soda $0.8 20
Cream Soda $0.8 20
Select a drink or enter exit to quit:
Drink Selected: Grape Soda
Enter amount:
Your change is $0.05
Cola $0.75 19
Root Beer $0.75 20
Lemon-Lime $0.75 20
Grape Soda $0.8 19
Cream Soda $0.8 20
Select a drink or enter exit to quit: exit
Machine has earned $1.55
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.