help fixing this code.  Its C++. When I try and run the program it doesn't work properly. I left a picture of what its supposed to do.  #include using namespace std; struct Drink { string drink_name;

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

I need help fixing this code.  Its C++. When I try and run the program it doesn't work properly. I left a picture of what its supposed to do. 

#include<bits/stdc++.h>
using namespace std;
struct Drink
{

string drink_name;
double drink_cost;
int drink_count;
};
//Drink_Simulator class.
class
Drink_Simulator
{

//public Block.
private:
//creating Drink array of 5 drinks
struct Drink d[5] = {
{"Cola", 1.00, 20},
{"Root beer", 1.00, 20},
{"Orange soda", 1.00, 20},
{"Grape soda", 1.00, 20},
{"Bottled water", 1.50, 20},
};

//
double inputMoney(string drink)
{

double amount;

cout << "Please enter the money for " << drink << ":;

cin >> amount;

for (int i = 0; i < 5; i++)
{
if (d[i].drink_name == drink)
{
if (d[i].drink_name <= amount)
{
return amount;
}
else
{
cout << "Enter a valid amount greater than equal to"
<< d[i].drink_cost << endl;
return -amount;
}
}
}
}
//public Block
public:
//displayChoices: display all choices of drinks and their cost.
void displayChoices()
{
cout << "Drink ame Cost" << endl;
cout << d[0].drink_name << " " << d[0].drink_cost << endl;
}
for (int i = 1; i < 5; i++)
{
cout << d[i].drink_name << "
"<<d[i].drink_cost<<endl;
}
}
//buyDrink: it handles a sale.
double buyDrink(string drink) {
double amount_entered = inputMoney(drink);
for (int i = 0; i < 5; i++) {
if (d[i].drink_name == drink) {
if (d[i].drink_count == 0) {
cout << "sold out" << endl;
return amount_entered;
}
else {
if (amount_entered < 0) {
return amount_entered;
}
cout << "Drink Available: " << endl;
cout << "The amount entered is valid,
do you want to buy the drink(yes or no) : ";
string ch;
cin >> ch;
if (ch == "no") {
return amount_entered;
}
cout << "Take your drink:
"<<drink<<endl;
d[i].drink_count -= 1;

return amount_enteredd[i].drink_cost;
d[i].drink_cost;
}
}
}
}
};//Drink_Simulator Class Ends.
//Main begins here
int main(void) {
//Drink_Simulator class object
Drink_Simulator ds;
//For drink choice
int flag = 0, vflag = 0;
double left_amount;
string drink_Choice;
//calling getData to get data from the user
while (flag < 5)
{
flag = 0;
}
cout << " Drink Options available" << endl;
ds.displayChoices();
std::cout << "Enter the drink option and Exit for
exiting the system : ";
std::getline(std::cin, drink_Choice);
if (drink_Choice == "Cola")
left_amount = ds.buyDrink("Cola");
else if (drink_Choice == "Root beer")
left_amount = ds.buyDrink("Root beer");
else if (drink_Choice == "Orange soda")
left_amount = ds.buyDrink("Orange soda");
else if (drink_Choice == "Grape soda")
left_amount = ds.buyDrink("Grape soda");
else if (drink_Choice == "Bottled water")
left_amount = ds.buyDrink("Bottled water");
else if (drink_Choice == "Exit")
exit(0);
cout << "And take your left amount:
"<<fabs(left_amount)<<endl;
flag++;
}
}//Main ends

 

 

Create a class that simulates and manages a soft drink machine. Information on each drink type should be stored in a structure that has data members to hold
the drink name, the drink price, and the number of drinks of that type currently in the machine.
The class should have an array of five of these structures, initialized with the following data
Drink Name Cost Number in Machine
Cola
1.00 20
Root beer
1.00 20
Orange soda 1.00 20
Grape soda 1.00 20
Bottled water 1.50 20
The class should have two public member functions, displayChoices (which displays a menu of drink names and prices) and buyDrink (which handles a sale). The
class should also have at least two private member functions, inputMoney, which is called by buyDrink to accept, validate, and return (to buyDrink) the amount of
money input, and dailyReport, which is called by the destructor to report how many of each drink type remain in the machine at the end of the day and how
much money was collected. You may want to use additional functions to make the program more modular.
The client program that uses the class should have a main processing loop that calls the displayChoices class member function and allows the patron to either
pick a drink or quit the program. If the patron selects a drink, the buyDrink class member function is called to handle the actual sale. This function should be
passed the patron's drink choice. Here is what the buyDrink function should do:
• Call the inputMoney function, passing it the patron's drink choice.
• If the patron no longer wishes to make the purchase, return all input money.
• If the machine is out of the requested soda, display an appropriate "sold out" message and return all input money.
• If the machine has the soda and enough money was entered, complete the sale by updating the quantity on hand and money collected information,
calculating any change due to be returned to the patron, and delivering the soda. This last action can be simulated by printing an appropriate "here is your
beverage" message.
Transcribed Image Text:Create a class that simulates and manages a soft drink machine. Information on each drink type should be stored in a structure that has data members to hold the drink name, the drink price, and the number of drinks of that type currently in the machine. The class should have an array of five of these structures, initialized with the following data Drink Name Cost Number in Machine Cola 1.00 20 Root beer 1.00 20 Orange soda 1.00 20 Grape soda 1.00 20 Bottled water 1.50 20 The class should have two public member functions, displayChoices (which displays a menu of drink names and prices) and buyDrink (which handles a sale). The class should also have at least two private member functions, inputMoney, which is called by buyDrink to accept, validate, and return (to buyDrink) the amount of money input, and dailyReport, which is called by the destructor to report how many of each drink type remain in the machine at the end of the day and how much money was collected. You may want to use additional functions to make the program more modular. The client program that uses the class should have a main processing loop that calls the displayChoices class member function and allows the patron to either pick a drink or quit the program. If the patron selects a drink, the buyDrink class member function is called to handle the actual sale. This function should be passed the patron's drink choice. Here is what the buyDrink function should do: • Call the inputMoney function, passing it the patron's drink choice. • If the patron no longer wishes to make the purchase, return all input money. • If the machine is out of the requested soda, display an appropriate "sold out" message and return all input money. • If the machine has the soda and enough money was entered, complete the sale by updating the quantity on hand and money collected information, calculating any change due to be returned to the patron, and delivering the soda. This last action can be simulated by printing an appropriate "here is your beverage" message.
Expert Solution
steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY