Working with structures, arrays, and classes

These are my requirements.

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
Coke .75 20
Root Beer .75 20
Orange Soda .75 20
Grape Soda .75 20
Bottled Water 1.00 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 which 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.
Input Validation: Only accept valid menu choices. Do not deliver a beverage if the
money inserted is less than the price of the selected drink.

This is what I have so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include "Drink.h"

using namespace std;

int main()
{
	Drink vending;
	int choice;

	choice = vending.displayChoices();
	vending.buyDrink(choice);

	return 0;
}


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
// Class Drink implmentation code "Drink.h"
#include <iostream>
using namespace std;

struct Drink
{
	// Public constructor / with default constructor
	Drink(string n, double c, int co)
	{
		name = n;
		cost = c;
		count = co;
	}
	Drink()
	{
		name = " ";
		cost = 0;
		count = 0;
	}

	const int DRINKS = 5; // Max number of drinks

	// New array using structure constructor to fill in data for all the drinks, costs, and inventory
	int data[DRINKS] =	 {Drink("Coke", .75, 20), 
						  Drink("Root Beet", .75, 20),
						  Drink("Orange Soda", .75, 20),
						  Drink("Grape Soda", .75, 20),
						  Drink("Bottled Water", 1.00, 20)};

	// Local variables
	string name; // Name of the drink ie: Coke
	double cost; // Cost ie: $.75
	int count; // Number of items in vending machine

	// Function prototypes
	int displayChoices();
	double buyDrink(int c); // Choice as argument so it knows what drink to process
	double inputMoney(int c); // Choice as argument so it knows what drink to process
};

// Code implmentation for the functions

int Drink::displayChoices()
{
	int choice;

	do
	{
		cout << "This is a vending machine program.\n"
			 << "1. Coke\n"
			 << "2. Root Beer\n"
			 << "3. Orange Soda\n"
			 << "4. Grape Soda\n"
			 << "5. Bottled Water\n"
			 << "6. Quit the Program\n"
			 << "Enter choice: ";
		cin >> choice;

		// Validate the choice

		while (choice < 1 || choice > 6)
		{
			cout << "Please select items 1-6"
				 << "Re-enter: ";
			cin >> choice;

			return choice;
		}

	} while (choice != 6);
}

double buyDrink(int choice)
{
	cout << "You selected #" << choice << "."
}

double inputMoney(int choice)
{
	int money,
		total;

	cout << "You selected #" << choice << "."
		 << "Enter 1 to insert a quarter or 0 to quit.\n";
	cin >> money;

	while (money != 0);
	{
		total += .25;

		cout << "Total change entered: " << total << "."
			 << "Enter 1 to insert a quarter or 0 to quit.\n";
		cin >> money;
	}

	return money;
}


My question is, does my array of structures need to be implemented inside the class Drink or my main program?

Also, how do I access the array of initialized information once I initialize it.

And, other thoughts and opinions on this process would be most appreciated.

Thank you.
Last edited on
you could make a class "vending" that has five memebers (coke, beer, orange and grape soda, water) of type Drink. Also your program is nearly complete, you just need to finish the buyDrink function and change things up a bit by adding the vending class and changing the imput type for buyDrink and inputMoney and the return type of displayChoices to Drink. also theese finctions should be members of the class vending. And, i think line 87 should (after all the improvements) be while (money<choice.cost). Oh, and I just noticed that you never declare name, cost or count, you just assign them.
Is this a class assignment your own personal project?
Here is the completed code. Let me know how I can reduce the work load / code by using for loops, if possible.

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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
Author: Blair Davidson
Date: 5/03/2012
Teacher: Professor
Assignment: Chapter 8 Lab 13
Description:
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
Coke			.75			20
Root Beer		.75			20
Orange Soda		.75			20
Grape Soda		.75			20
Bottled Water	1.00		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 which 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.
Input Validation: Only accept valid menu choices. Do not deliver a beverage if the
money inserted is less than the price of the selected drink.
*/

#include <iostream>
#include <string>
#include "Drink.h"

using namespace std;

int main()
{
	Drink vending;
	int choice,
		loopChoice;
	double moneyBack;

	
	const int DRINKS = 5; // Max number of drinks

	// New array using structure constructor to fill in data for all the drinks, costs, and inventory
	Drink data[DRINKS] = {Drink("Coke", .75, 20), 
						  Drink("Root Beet", .75, 20),
						  Drink("Orange Soda", .75, 20),
						  Drink("Grape Soda", .75, 20),
						  Drink("Bottled Water", 1.00, 20)};
	cout << "To use the vending machine enter 1.\n"
	     << "Otherwise, enter 0 to quit.\n";
	cin >> loopChoice;

	while (loopChoice != 0)
	{
		choice = vending.displayChoices();
		moneyBack = vending.buyDrink(choice);

		switch (choice)
		{
		case 1:
		
			if (data[0].count > 0)
				data[0].count = vending.dailyReport(moneyBack, data[0].count);
				cout << data[0].name << endl << data[0].cost << endl 
					 << data[0].count << endl;
			break;

		case 2:
		
			if (data[1].count > 0)
				data[1].count = vending.dailyReport(moneyBack, data[1].count);
				cout << data[1].name << endl << data[1].cost << endl 
					 << data[1].count << endl;
			break;

		case 3:
		
			if (data[2].count > 0)
				data[2].count = vending.dailyReport(moneyBack, data[2].count);
				cout << data[2].name << endl << data[2].cost << endl 
					 << data[2].count << endl;
			break;

		case 4:
		
			if (data[3].count > 0)
				data[3].count = vending.dailyReport(moneyBack, data[3].count);
				cout << data[3].name << endl << data[3].cost << endl 
					 << data[3].count << endl;
			break;

		case 5:
		
			if (data[4].count > 0)
				data[4].count = vending.dailyReport(moneyBack, data[4].count);
				cout << data[4].name << endl << data[4].cost << endl 
					 << data[4].count << endl;
			break;

		default:
			break;
		}

	cout << "If you would like to make another purchase"
		 << " enter 1 to continue or 0 to quit.\n";
	cin >> loopChoice;

	}

	return 0;
}

/*
Output:

To use the vending machine enter 1.
Otherwise, enter 0 to quit.
1
This is a vending machine program.
   Item                 Cost
1. Coke                 .75
2. Root Beer            .75
3. Orange Soda          .75
4. Grape Soda           .75
5. Bottled Water        1.00
6. Quit the Program
Enter choice: 1
You selected Coke.
Your item costs $0.75.
Enter 0.75 to insert your cash or 0 to quit.
1
Your change is $0.25.
Thank you for your purchase.
Coke
0.75
19
If you would like to make another purchase enter 1 to continue or 0 to quit.
1
This is a vending machine program.
   Item                 Cost
1. Coke                 .75
2. Root Beer            .75
3. Orange Soda          .75
4. Grape Soda           .75
5. Bottled Water        1.00
6. Quit the Program
Enter choice: 2
You selected Root Beer.
Your item costs $0.75.
Enter 0.75 to insert your cash or 0 to quit.
1
Your change is $0.25.
Thank you for your purchase.
Root Beet
0.75
19
If you would like to make another purchase enter 1 to continue or 0 to quit.
1
This is a vending machine program.
   Item                 Cost
1. Coke                 .75
2. Root Beer            .75
3. Orange Soda          .75
4. Grape Soda           .75
5. Bottled Water        1.00
6. Quit the Program
Enter choice: 1
You selected Coke.
Your item costs $0.75.
Enter 0.75 to insert your cash or 0 to quit.
1
Your change is $0.25.
Thank you for your purchase.
Coke
0.75
18
If you would like to make another purchase enter 1 to continue or 0 to quit.
*/

Here is the header Drink.h

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
// Class Drink implmentation code "Drink.h"
#include <iostream>
#include <iomanip>
using namespace std;

struct Drink
{
	// Public constructor / with default constructor
	Drink(string n, double c, int co)
	{
		name = n;
		cost = c;
		count = co;
	}
	Drink()
	{
		name = " ";
		cost = 0;
		count = 0;
	}

	// Local variables
	string name; // Name of the drink ie: Coke
	double cost; // Cost ie: $.75
	int count; // Number of items in vending machine

	// Function prototypes
	int displayChoices();
	double buyDrink(int c); // Choice as argument so it knows what drink to process
	double inputMoney(double c); // Cost as argument so it knows how much to collect
	int dailyReport(double m, int i); // MoneyBack as arugment so it knows how to process quantity
};

// Code implmentation for the functions

int Drink::displayChoices()
{
	int choice;

	cout << "This is a vending machine program.\n"
		 << setw(7) << "Item" << setw(22) << "Cost\n"
		 << "1. Coke\t\t\t.75\n"
		 << "2. Root Beer\t\t.75\n"
		 << "3. Orange Soda\t\t.75\n"
		 << "4. Grape Soda\t\t.75\n"
		 << "5. Bottled Water\t1.00\n"
		 << "6. Quit the Program\n"
		 << "Enter choice: ";
	cin >> choice;

	// Validate the choice
	while (choice < 1 || choice > 6)
	{
		cout << "Please select items 1-6"
			 << "Re-enter: ";
		cin >> choice;
	}

	return choice;
}

double Drink::buyDrink(int choice)
{
	string tempName;

	switch (choice)
	{
	case 1:
		tempName = "Coke";
		break;

	case 2:
		tempName = "Root Beer";
		break;

	case 3:
		tempName = "Orange Soda";
		break;

	case 4:
		tempName = "Grape Soda";
		break;

	case 5:
		tempName = "Bottled Water";
		break;

	default:
		tempName = " ";
		break;
	}

	cout << "You selected " << tempName << ".\n";
	double cost,
		   moneyBack; // the cost argument returned back from inputMoney

	if (choice >= 1 && choice <= 4)
	{
		cost = .75;
		moneyBack = inputMoney(cost);
		return moneyBack;
	}
	else
	{
		cost = 1.00;
		moneyBack = inputMoney(cost);
		return moneyBack;
	}
}

double Drink::inputMoney(double cost)
{
	double money = 0;

	if (cost == .75)
	{
		cout << "Your item costs $" << cost << ".\n"
			 << "Enter " << cost << " to insert your cash or 0 to quit.\n";
		cin >> money;

		if (money > cost)
		{
			money = money - cost;
			cout << "Your change is $" << money << ".";
		}

		return money;
	}

	else if (cost == 1.00)
	{
		cout << "Your item costs $" << cost << ".\n"
			 << "Enter " << cost << " to insert your cash or 0 to quit.\n";
		cin >> money;

		if (money > cost)
		{
			money = money - cost;
			cout << fixed << setprecision(2) << "Your change is $" << money << ".\n";
		}

		return money;
	}

	if (money == 0 && money < cost)
	{
		money = 0;

		cout << "You have decided to cancel your purchase.\n"
			 << "It is done so.\n";

		return money;
	}

	return money;
}

int Drink::dailyReport(double moneyBack, int count)
{
	if (moneyBack > 0)
	{
		cout << "\nThank you for your purchase.\n";
		count--;
		return count--;
	}
	else
	{
		cout << "Come back next time!\n";
		return count;
	}

}


If anyone needs this please e-mail me and let me know that you have used it, it would be nice for people to give thanks or token of appreciation as I am a learning student among learning students and it's nice to hear.

Also if you come across this searching google, it is likely you are doing Starting with C++ 7th Edition, don't hesitate to e-mail me over other lab concerns as well.

Any code improvements, please let me know too. Thanks.
Topic archived. No new replies allowed.