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;
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


Step by step
Solved in 3 steps with 2 images









