chocolate vending machine errors

On line 32, i am having an error saying '<':signed/unsigned mismatch, can anybody please tell me why this is? also, from lines 98 it is saying 'Chocolate_Dispenser' is an undeclared identifier, can anyone help me understand why this is please?
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
enum state { Out_Of_Chocolate, No_Credit, Has_Credit, Dispenses_Chocolate, Maintenance_Mode };
#include <vector>
#include <iostream>

using namespace std;

class StateContext;

class State
{
protected:
	StateContext* CurrentContext;
public:
	State(StateContext* Context) { CurrentContext = Context; }
	virtual ~State(void) {}
};

class StateContext
{
protected:
	State* CurrentState = nullptr;
	int stateIndex = 0;
	vector<State*> availableStates;
public:
	virtual ~StateContext(void);
	virtual void setState(state newState);
	virtual int getStateIndex(void);
};

StateContext::~StateContext(void)
{
	for (int i = 0; i < this->availableStates.size(); i++) delete this->availableStates[i];
	this->availableStates.clear();
}

void StateContext::setState(state newState)
{
	this->CurrentState = availableStates[newState];
	this->stateIndex = newState;
}


int StateContext::getStateIndex(void)
{
	return this->stateIndex;
}

class Transition
{
public:
	virtual bool insertMoney(int) { cout << "Error!" << endl; return false; }
	virtual bool makeSelection(int) { cout << "Error!" << endl; return false; }
	virtual bool moneyRejected(void) { cout << "Error!" << endl; return false; }
	virtual bool addChocolate(int) { cout << "Error!" << endl; return false; }
	virtual bool dispense(void) { cout << "Error!" << endl; return false; }
	virtual bool enterPin(int pin) { cout << "Error!" << endl; return false; }
	virtual bool exit(void) { cout << "Error!" << endl; return false; }
};

class ChocoState : public State, public Transition
{
public:
	ChocoState(StateContext* Context) : State(Context) {}
};

class OutOfChocolate : public ChocoState
{
public:
	OutOfChocolate(StateContext* Context) : ChocoState(Context) {}
	bool addChocolate(int number);
};

class NoCredit : public ChocoState
{
public:
	NoCredit(StateContext* Context) : ChocoState(Context) {}
	bool insertMoney(int credit);
};

class HasCredit : public ChocoState
{
public:
	HasCredit(StateContext* Context) : ChocoState(Context) {}
	bool insertMoney(int credit);
	bool makeSelection(int option);
	bool moneyRejected(void);
};

class DispensesChocolate : public ChocoState
{
public:
	DispensesChocolate(StateContext* Context) : ChocoState(Context) {}
	bool dispense(void);
};

bool OutOfChocolate::addChocolate(int number)
{
	((Chocolate_Dispenser*)CurrentContext)->inventory += number;
	cout << "Adding chocolate... Inventory = " << ((Chocolate_Dispenser*)CurrentContext)->inventory << endl;
	CurrentContext->setState(No_Credit);
	return true;
}
bool NoCredit::insertMoney(int credit)
{
	((Chocolate_Dispenser*)CurrentContext)->credit += credit;
	cout << "Adding credit... credit = " << ((Chocolate_Dispenser*)CurrentContext)->credit << endl;
	CurrentContext->setState(Has_Credit);
	return true;
}

bool HasCredit::insertMoney(int credit)
{
	((Chocolate_Dispenser*)CurrentContext)->credit += credit;
	cout << "Adding credit... Credit = " << ((Chocolate_Dispenser*)CurrentContext)->credit << endl;
	return true;
}

bool HasCredit::makeSelection(int option)
{
	//the option will be equal to the number of bars that the customer has selected 
	cout << "You have selected " << option << " bar(s) of chocolate" << endl;

	if (((Chocolate_Dispenser*)CurrentContext) ->inventory < option)
	{
		cout << "Error: you have selected more chocolate than the machine contains" << endl;
		return true;
	}

	if (((Chocolate_Dispenser*)CurrentContext)->credit < option)
	{
		cout << "Error: you don't have enough credit for that selection" << endl;
		return true;
	}
	cout << "Credit and inventory is sufficient for your selection" << endl;

	((Chocolate_Dispenser*)CurrentContext)->inventory -= option; //deduct inventory
	((Chocolate_Dispenser*)CurrentContext)->credit -= option; //deduct credit 

	CurrentContext->setState(Dispenses_Chocolate);
	return true;
}

bool HasCredit::moneyRejected(void)
{
	cout << "Rejectng money...." << endl;
	((Chocolate_Dispenser*)CurrentContext)->credit = 0;
	CurrentContext->setState(No_Credit);
	return true;
}

bool DispensesChocolate::dispense(void)
{
	cout << "Dispensing..." << endl;
	cout << "Inventory = " << ((Chocolate_Dispenser*)CurrentContext)->inventory << endl;
	cout << "Credit = " << ((Chocolate_Dispenser*)CurrentContext)->credit << endl;

	if (((Chocolate_Dispenser*)CurrentContext)->inventory == 0) CurrentContext->setState(Out_Of_Chocolate);
	else if (((Chocolate_Dispenser*)CurrentContext)->credit == 0) CurrentContext->setState(No_Credit);
	else CurrentContext->setState(Has_Credit);
	return true;
}

class Chocolate_Dispenser : public StateContext, public Transition
{
	friend class OutOfChocolate;
	friend class NoCredit;
	friend class HasCredit;
	friend class DispensesChocolate;
private:
	int inventory = 0; //number of chocolate 
	int credit = 0; //measure of the number of bars that can be purchased and not money
public:
	Chocolate_Dispenser(void);
	bool insertMoney(int credit);
	bool makeSelection(int option);
	bool moneyRejected(void);
	bool enterPin(int pin);
	bool exit(void);
	bool addChocolate(int number);
	bool dispense(void);
};

Chocolate_Dispenser::Chocolate_Dispenser(void)
{
	this->availableStates.push_back(new OutOfChocolate(this));
	this->availableStates.push_back(new NoCredit(this));
	this->availableStates.push_back(new HasCredit(this));
	this->availableStates.push_back(new DispensesChocolate(this));

	this->setState(Out_Of_Chocolate);
}


bool Chocolate_Dispenser::insertMoney(int credit)
{
	return ((ChocoState*)CurrentState)->insertMoney(credit);
}
bool Chocolate_Dispenser::makeSelection(int option)
{
	return ((ChocoState*)CurrentState)->makeSelection(option);
}
bool Chocolate_Dispenser::moneyRejected(void)
{
	return ((ChocoState*)CurrentState)->moneyRejected();
}
bool Chocolate_Dispenser::addChocolate(int number)
{
	return ((ChocoState*)CurrentState)->addChocolate(number);
}
bool Chocolate_Dispenser::dispense(void)
{
	return ((ChocoState*)CurrentState)->dispense();
}
bool Chocolate_Dispenser::enterPin(int pin)
{
	return ((ChocoState*)CurrentState)->enterPin(pin);
}
bool Chocolate_Dispenser::exit(void)
{
	return ((ChocoState*)CurrentState)->exit();
}

int main(void)
{
	Chocolate_Dispenser MyDispenser;

	MyDispenser.addChocolate(10);
	MyDispenser.makeSelection(2);
	MyDispenser.insertMoney(10);
	MyDispenser.makeSelection(20);
	MyDispenser.makeSelection(10);
	MyDispenser.dispense();
	MyDispenser.insertMoney(10);
	MyDispenser.enterPin(4);
	MyDispenser.exit(); 


	return 0;
}
Last edited on
Please don't start a new topic on the same subject - it's a time waster for those who reply.

http://www.cplusplus.com/forum/general/212192/
1) As TheIdeasMan says, please don't start multiple threads on the same topic.

2)
On line 32, i am having an error saying '<':signed/unsigned mismatch, can anybody please tell me why this is?

You've declared i to be a signed int. std::vector::size() returns a size_t, which is unsigned.

3)
also, from lines 98 it is saying 'Chocolate_Dispenser' is an undeclared identifier, can anyone help me understand why this is please?

You don't define Chocolate_Dispenser until later in the file. At the point where the compiler tries to process line 98, it has no idea what that symbol means.
Last edited on
Topic archived. No new replies allowed.