drink machine simulator help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include<iostream>
#include<iomanip>
#include<string>
#include<cmath>

using namespace std;
const int SIZE = 5;

struct DrinkingMachine
{
	string name;
	double price;
	double money;
	double totalMoney;
	int inventory;
	DrinkingMachine()
	{     
		name = "";
		price = 0.00;
		inventory = 0;
	}

	int DrinkMachine(string n, double p, int i)
	{
		name = n;
		price = p;
		inventory = i;
	}
};

class DrinkManage {
private:
	DrinkingMachine drinks[SIZE];
	double sales;
	double inputMoney(int);


public:
	void displayChoices();
	void buyDrink(int n);
	void setup();
	double dailyReport();

};

void DrinkManage::setup()
{

	drinks[0].name = "Cola                 $";
	drinks[1].name = "Root Beer            $";
	drinks[2].name = "Lemon-Lime           $";
	drinks[3].name = "Grape Soda           $";
	drinks[4].name = "Water                $";
	for (int i = 0; i < SIZE; i++)
	{
		drinks[0].price = 0.65;
		drinks[1].price = 0.70;
		drinks[2].price = 0.75;
		drinks[3].price = 0.85;
		drinks[4].price = 0.90;
		drinks[i].inventory = 20;

	}
}

void DrinkManage::displayChoices()
{
	int bevChoice;

	
	for (int i = 0; i < SIZE; i++)
		cout << (i + 1)
		<< ") " << drinks[i].name
		<< setw(5) << setprecision(3)
		<< drinks[i].price << setw(10) << "	Qty:  "
		<< drinks[i].inventory << endl;
	cout << "\nSelect a drink(1-5) or enter 6 to quit: ";
	cin >> bevChoice;

	while (bevChoice < 0 || bevChoice > 6)
	{
		cout << "Invalid Entry. Please enter a value 1-5 " << endl;
		cin >> bevChoice;
	}
	if (bevChoice > 0 && bevChoice < 6)
	{
		bevChoice--;
		buyDrink(bevChoice);
	}
	
}

void DrinkManage::buyDrink(int bevChoice)
{
	double money, change, sales;
	bevChoice = bevChoice;

	money = inputMoney(bevChoice);
	if (money > 0)
	{
		change = (money - drinks[bevChoice].price);

		if (drinks[bevChoice].inventory > 0)
		{
			drinks[bevChoice].inventory -= 1;

			money -= change;

			sales = money;

			if (change > 0)
			{
				cout << "Change: $" << change << endl;
			}
		}
	else
	{
		cout << "SOLD OUT" << endl;
	}

	}

}



double DrinkManage::inputMoney(int bevChoice)
{
	double money;



	cout << fixed << showpoint << setprecision(2);
	cout << "Enter the amount of money you want to insert: ";
	cin >> money;

	if (money < drinks[bevChoice].price)
	{
		cout << "Insufficient Funds." << endl;
	}
	return money;
}

double DrinkManage::dailyReport()
{
	double sales = 0;

	cout << "Drink          Number Left \n";

	for (int i = 0; i < 5; i++)
	{
		cout << setw(10) << drinks[i].name << "         " << drinks[i].inventory << endl;
	}

	cout << "Total Revenue: " << sales << endl;

	return sales;
}

int main()
{

	DrinkManage check;
	char beverage;
	check.setup();
	do
	{
		check.displayChoices();

		cout << "Do you want to make another purchase? Please enter Y or N: ";
		cin >> beverage;

	} while (beverage == 'Y' || beverage == 'y');


	return 0;
}

// this is a program that simulates a soft drink machine. it displays a table of drink name, cost and number of drink available in machine which is 20,in constant. the user should be allowed to quit the program or select a drink. it also keeps track of total sales and displays an updated table.



I designed my loop with a while loop with a question " do you still want to continue, yes or no?"
i wanna learn how to make my program go on a loop by just the " select a number 1-5 or enter 6". i don't know how to revise my loop just so it'll quit when user enters '6' and then shows the "total sales" drinkManage :: dailyReport()

Please help me! I wanna remove the loop "do you still want to continue?" on my main function.
Last edited on
closed account (48T7M4Gy)
Do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct DrinkingMachine
{
    string name;
    double price;
    double money;
    double totalMoney;
    int inventory;
    DrinkingMachine()
    {
        name = "";
        price = 0.00;
        inventory = 0;
    }
    
    DrinkingMachine(string n, double p, int i)
    {
        name = n;
        price = p;
        inventory = i;
    }
};


Get this:
1) Cola  0.65  20
2) Root Beer   0.7  20
3) Lemon-Lime  0.75  20
4) Grape Soda  0.85  20
5) Water   0.9  20

Select a drink(1-5) or enter 6 to quit:

Last edited on
also how do i add up the total revenue? mine keeps popping out as 0
closed account (48T7M4Gy)
mine keeps popping out as 0

Sounds bad.
Topic archived. No new replies allowed.