Question

NOTE #1: NO GLOBALS, In addition programs with infinite while(true) loops or improper breaks etc. NOTE...

NOTE #1: NO GLOBALS, In addition programs with infinite while(true) loops or improper breaks etc.

NOTE #2: MAKE YOUR PROGRAM AS EFFICIENT AS POSSIBLE.

Drink Machine Simulator - Based on Structures Lecture

Write a c++ 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 choose the 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.

View required output

Test Case 1

Standard Input                
1ENTER
.9ENTER
7ENTER
7ENTER
1ENTER
.5ENTER
1ENTER
4ENTER
1ENTER
2ENTER
1ENTER
5ENTER
.95ENTER
3ENTER
.2ENTER
.8ENTER
6
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.25\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Invalid selection. Please try again.\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Invalid selection. Please try again.\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: Please enter an amount between $0.00 and $1.00.\n
It should also be equal to or greater than the drink price : \n
CHANGE : $0.35\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.15\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.30\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.05\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: Please enter an amount between $0.00 and $1.00.\n
It should also be equal to or greater than the drink price : \n
CHANGE : $0.05\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: \n
SOFT DRINK MACHINE REPORT\n
--------------------------\n
Total amount earned: $4.50\n
\n

Test Case 2

Standard Input                
1ENTER
.9ENTER
1ENTER
.9ENTER
1ENTER
.9ENTER
1ENTER
.9ENTER
1ENTER
.9ENTER
1ENTER
.9ENTER
1ENTER
.9ENTER
1ENTER
.9ENTER
1ENTER
.9ENTER
1ENTER
.9ENTER
1ENTER
.9ENTER
1ENTER
.9ENTER
1ENTER
.9ENTER
1ENTER
.9ENTER
1ENTER
.9ENTER
1ENTER
.9ENTER
1ENTER
.9ENTER
1ENTER
.9ENTER
1ENTER
.9ENTER
1ENTER
.9ENTER
1ENTER
1ENTER
1ENTER
1ENTER
1ENTER
1ENTER
2ENTER
1ENTER
3ENTER
1ENTER
6
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.25\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.25\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.25\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.25\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.25\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.25\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.25\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.25\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.25\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.25\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.25\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.25\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.25\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.25\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.25\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.25\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.25\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.25\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.25\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.25\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Sorry. This drink is sold out.\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Sorry. This drink is sold out.\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Sorry. This drink is sold out.\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Sorry. This drink is sold out.\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Sorry. This drink is sold out.\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Sorry. This drink is sold out.\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.30\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: Please enter the amount you want to insert: \n
CHANGE : $0.25\n
\n
SOFT DRINK MACHINE\n
------------------\n
1) Cola ($0.65)\n
2) Root Beer ($0.70)\n
3) Lemon-Lime ($0.75)\n
4) Grape Soda ($0.85)\n
5) Water ($0.90)\n
6) Quit Program\n
\n
Please make a selection: \n
SOFT DRINK MACHINE REPORT\n
--------------------------\n
Total amount earned: $14.45\n
\n
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

// Drink structure
struct Drink
{
string name;
double cost;
int number;
};

// Method which displays menu
// Asks user to enter a choice
// Validates the choice
// And returns it
int menu()
{
int choice;
cout<<"SOFT DRINK MACHINE\n"<<endl;
cout<<"------------------\n"
"1) Cola ($0.65)\n"
"2) Root Beer ($0.70)\n"
"3) Lemon-Lime ($0.75)\n"
"4) Grape Soda ($0.85)\n"
"5) Water ($0.90)\n"
"6) Quit Program\n"<<endl;

cout<<"Please make a selection: ";
cin>>choice;

// If entered choice is not between 1-6, ask user to enter it again
while(choice<1 || choice>6)
{
cout<<"Invalid selection. Please try again."<<endl;
cout<<"Please make a selection: ";
cin>>choice;
}
return choice;
}

int main()
{
// Create 5 drink structures
// Assign values to their variables
Drink d1,d2,d3,d4,d5;
d1.name = "Cola";
d1.cost = 0.65;
d1.number = 20;

d2.name = "Root Beer";
d2.cost = 0.70;
d2.number = 20;

d3.name = "Lemon-Lime";
d3.cost = 0.75;
d3.number = 20;

d4.name = "Grape Soda";
d4.cost = 0.85;
d4.number =20;

d5.name = "Water";
d5.cost = 0.90;
d5.number = 20;

// Initialize the array of structure with above drink structures
Drink drinkArray[5] = {d1,d2,d3,d4,d5};

// Local Variables
double total_amount_earned = 0;
int choice = menu();

// Iterate while loop till choice is not 6
while(choice!=6)
{
// Get the number of available drinks for selected drink
int number_of_drink_available = drinkArray[choice-1].number;

// If it's 0, display message
if(number_of_drink_available==0)
{
cout<<"Sorry. This drink is sold out.\n"<<endl;
}
// Else,
else
{
// Get the cost of selected drink
double drink_cost = drinkArray[choice-1].cost;

// Ask user to enter amount
double amount;
cout<<"Please enter the amount you want to insert:";
cin>>amount;

// If entered amount is not between 0-1
// and if it's less than cost of drink
// Ask user to enter the amount again
while(amount<0 || amount>1 || amount<drink_cost)
{
cout<<"Please enter an amount between $0.00 and $1.00.\n"
"It should also be equal to or greater than the drink price :";
cin>>amount;
}

// Add drink_cost to total_amount_earned
// Decrement number of drinks by 1
// Calculate change
// Display change
total_amount_earned+=drink_cost;
drinkArray[choice-1].number-=1;
double change = amount-drink_cost;
cout<<"CHANGE: $"<<fixed<<setprecision(2)<<change<<"\n"<<endl;
}
// Call menu again
choice = menu();
}

// Print total amount earned by machine
cout<<"SOFT DRINK MACHINE REPORT\n--------------------------"<<endl;
cout<<"Total amount earned: $"<<fixed<<setprecision(2)<<total_amount_earned<<endl;
return 0;
}

OUTPUT-

SOFT DRINK MACHINE 1) Cola ($0.65) 2) Root Beer ($0.70) 3) Lemon-Lime ($0.75) 4) Grape Soda ($0.85) 5) Water ($e.90) 6) Quit Program Please make a selection: 1 Please enter the amount you want to insert:.9 CHANGE: $0.25 SOFT DRINK MACHINE 1) Cola ($0.65) 2) Root Beer ($0.70) 3) Lemon-Lime ($0.75) 4) Grape Soda ($0.85) 5) Water ($e.90) 6) Quit Program Please make a selection: 7 Invalid selection. Please try again Please make a selection: 7 Invalid selection. Please try again Please make a selection: 1 Please enter the amount you want to insert:.5 Please enter an amount between $0.00 and $1.00. It should also be equal to or greater than the drink price :1 CHANGE: $0.35 SOFT DRINK MACHINE 1) Cola ($0.65) 2) Root Beer ($0.70) 3) Lemon-Lime ($0.75) 4) Grape Soda ($0.85)

Add a comment
Know the answer?
Add Answer to:
NOTE #1: NO GLOBALS, In addition programs with infinite while(true) loops or improper breaks etc. NOTE...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • q13 c++ if answering please do not write code in paragrapth form. 13. Drink Machine Simulator...

    q13 c++ if answering please do not write code in paragrapth form. 13. Drink Machine Simulator 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 Cola .75 20 Root Beer .75 20 Lemon-Lime .75 20 Grape...

  • I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include...

    I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include <string> #include <iomanip> using namespace std; struct Drink {    string name;    double cost;    int noOfDrinks; }; void displayMenu(Drink drinks[], int n); int main() {    const int size = 5;       Drink drinks[size] = { {"Cola", 0.65, 2},    {"Root Beer", 0.70, 1},    {"Grape Soda", 0.75, 5},    {"Lemon-Lime", 0.85, 20},    {"Water", 0.90, 20} };    cout <<...

  • Drink Machine Simulator C++

    Drink Machine SimulatorThe purpose of this exercise is to give you practice with abstract data types, namely structures and arrays of structures.Write a program that simulates a softdrink machine. The program should use a structure that stores the following data:Drink NameDrink CostNumber of Drinks in MachineThe program should create an array of five structures. The elements should be initialized with the following data:Drink Name Cost Number in MachineCoca-Cola .75 20Root Beer .75 20Sprite .75 20Spring Water .80 20Apple Juice .80...

  • Write a program in C++ that simulates a soft drink machine. The program will need several...

    Write a program in C++ that simulates a soft drink machine. The program will need several classes: DrinkItem, DrinkMachine and Receipt. For each of the classes you must create the constructors and member functions required below. You can, optionally, add additional private member functions that can be used for doing implementation work within the class. DrinkItem class The DrinkItem class will contains the following private data members: name: Drink name (type of drink – read in from a file). Type...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT