Calling Functions

Hi. I'm not sure how to call displayUsersChoice properly inside main. Thanks!

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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
 #include <iostream>
#include <string>
using namespace std;


class CashRegister
{
private:
	int moneyInRegister;

public:
	CashRegister();
	CashRegister(int);
	int getCurrentAmount() const;
	void setCurrentAmount(int);

};

CashRegister::CashRegister(){
	moneyInRegister = 300;

}

CashRegister::CashRegister(int money){
	if (money >= 0)
	{
		moneyInRegister = money;
	}
	else
	{
		moneyInRegister = 300;
	}
}

int CashRegister::getCurrentAmount() const{
	return moneyInRegister;
}
void CashRegister::setCurrentAmount(int money){
	moneyInRegister = moneyInRegister + money;
}

class CandyDispenser
{
private:
	int numberOfItems;
	int amount;
public:
	CandyDispenser(int, int);
	int getNumberOfItems() const;
	void setNumberOfItems(int);
	int getAmount() const;
	void setAmount(int);
	void sellCandy();
};

CandyDispenser::CandyDispenser(int items, int setAmount){
	if (items >= 0)
		numberOfItems = items;
	else
		items = 100;
	if (setAmount >= 0)
		amount = setAmount;
	else
		amount = 100;

}

void CandyDispenser::setNumberOfItems(int items){
	numberOfItems = items;
}

int CandyDispenser::getNumberOfItems() const{
	return numberOfItems;
}
/*int CandyDispenser::setAmount(int amt){
	amount = amt;
}*/
int CandyDispenser::getAmount() const{
	return amount;
}
void CandyDispenser::sellCandy(){
	numberOfItems--;
}



class CandyShop
{
private:
	CashRegister counter;
public:
	CandyShop();
	void candyChoice();
	void displayUserChoice();
	void getCandyInput(CandyDispenser &, CashRegister &);
	
};

CandyShop::CandyShop(){
	counter = 0;
}

void CandyShop::candyChoice(){
	CandyDispenser candy(100, 5);
	CandyDispenser chips(100, 10);
	CandyDispenser gum(100, 75);
	CandyDispenser cookies(100, 65);
	
	int userChoice;
	cin >> userChoice;

	
	while (userChoice != 9) {
		switch (userChoice){
		case 1:
			getCandyInput(candy, counter);
			break;
		case 2:
			getCandyInput(chips, counter);
			break;
		case 3:
			getCandyInput(gum, counter);
			break;
		case 4:
			getCandyInput(cookies, counter);
			break;
		default:
			cout << "Invalid selection" << endl;
		}

		displayUserChoice();
		cin >> userChoice;
	}

}

void CandyShop::displayUserChoice(){
	cout << "***** Welcome to Shelly's Candy Shop *****" << endl;
	cout << "To select an item enter " << endl;
	cout << "1 for Candy " << endl;
	cout << "2 for Chipes" << endl;
	cout << "3 for Gum" << endl;
	cout << "4 for Cookies" << endl;
	cout << "9 to exit" << endl;
}
void CandyShop::getCandyInput(CandyDispenser &item, CashRegister &xCounter){
	int amt1, amt2;

	if (item.getNumberOfItems() > 0) {
		cout << "" << item.getAmount() << endl;
		cin >> amt1;

		if (amt1 < item.getAmount()){
			cout << "Please deposit anther " << item.getAmount() - amt1 << " cents" << endl;
			cin >> amt2;
			(amt1 + amt2);
		}

		if (amt1 >= item.getAmount()){
			xCounter.setCurrentAmount(amt1);
			item.sellCandy();
			cout << "Collect your item at the bottom and enjoy" << endl;
		}
		else{
			cout << "The amount is not enough.  " << endl;
			cout << "Collect what you deposited" << endl;
			cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << endl;

		}
	}
	else {
		cout << "Sorry this item is sold out" << endl;
	}
}
	


int main()
{
	
	
	CashRegister counter;
	CandyDispenser candy(100, 5);
	CandyDispenser chips(100, 10);
	CandyDispenser gum(100, 75);
	CandyDispenser cookies(100, 65);
	CandyShop getCandyInput(CandyDispenser, CashRegister);
	int userChoice;
	CandyShop displayUsersChoice();
	cin >> userChoice;

	while (userChoice != 9) {
		switch (userChoice){
		case 1:
			getCandyInput(candy, counter);
			break;
		case 2:
			getCandyInput(chips, counter);
			break;
		case 3:
			getCandyInput(gum, counter);
			break;
		case 4:
			getCandyInput(cookies, counter);
			break;
		default:
			cout << "Invalid selection" << endl;
		}

		CandyShop displayUserChoice();
		cin >> userChoice;
	}
	system("pause");
	return 0;

}.
I have another error. I'm not calling getCandyInput correctly.
I fixed the errors, but when it asks to deposit more money it's not calculating properly. I don't see an error in my logic. Can someone 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
	CandyShop candyType;
	CashRegister counter;
	CandyDispenser candy(100, 5);
	CandyDispenser chips(100, 10);
	CandyDispenser gum(100, 75);
	CandyDispenser cookies(100, 65);
	CandyShop getCandyInput(CandyDispenser, CashRegister);
	int userChoice;
	
	candyType.displayUserChoice();
	cin >> userChoice;

	while (userChoice != 9) {
		switch (userChoice){
		case 1:
			candyType.getCandyInput(candy, counter);
			break;
		case 2:
			candyType.getCandyInput(chips, counter);
			break;
		case 3:
			candyType.getCandyInput(gum, counter);
			break;
		case 4:
			candyType.getCandyInput(cookies, counter);
			break;
		default:
			cout << "Invalid selection" << endl;
		}

		candyType.displayUserChoice();
		cin >> userChoice;
	}
	system("pause");
	return 0;

}
It's not calculating properly

You should explain what you mean by this.

Just skimming over, one thing I notice is this
1
2
3
4
5
		if (amt1 < item.getAmount()){
			cout << "Please deposit anther " << item.getAmount() - amt1 << " cents" << endl;
			cin >> amt2;
			(amt1 + amt2);
		}

The line "amt1 + amt2;" doesn't do anything, perhaps you meant to assign it to something.
Topic archived. No new replies allowed.