Virtual/Cyber Pet Program

I'm a beginner at this and I'm trying to make a sort of cyber/virtual pet program, I am getting really confused as to how to make it work the way I want to. I want it to have a happiness, tiredness and hunger level which can be increased by playing with it (pressing P), feeding it (pressing F) or putting it to sleep (pressing S) at any time. Then get an update on how your pet is feeling. I've made a start and used enumerated types to create the different levels of hungry, tired, happy etc. and it sort of works, but it just cuts out after feeding it and making it sleep once, i want it to continually run until the user ends game or the pet dies from lack of care? not sure if that makes sense, i can post my code if necessary? thanks for any help!
Sounds like you need a loop. I would recommend a do-while loop for this one. And yes, post your code, but please use code tags for all your code - http://www.cplusplus.com/articles/jEywvCM9/
Thank you! Okay, here is the code I have so far :

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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <iostream>
#include <string>

using namespace std; 
string name; // creates variable for pet name to be stored as string
enum hunger { fed, peckish, hungry, starving, dead }; // creates all possible states for pets hunger levels
hunger currentState = hungry; // states current hunger level
enum tiredness { wide, awake, tired, falling, collapsed }; // creates all possible states for pets tiredness levels
tiredness current = awake; // states current tiredness level
char input; // creates variable to store user input as a character
int hungerhappy = 0;
int tiredhappy = 0;
int happinesslevel = 0;



string petname() // function for naming pet
{
	
	char option;

	cout << "Would you like to name your pet, Y or N" << endl;
	cin >> option; // stores user input as Y or N
	 
	if (option == 'Y') // if statement to determine pet name by user input
	{
		cout << "Enter your pets name" << endl;
		cin >> name;
	}
	else
	{
		name = "your pet";
	}
	return name;
}


int petstate() //function to determine pets hunger levels
{

switch (currentState) // creates different output for different hunger levels
{

case fed:
	cout << name << " is well-fed!" << endl;
	break;
case peckish:
	cout << name << " is feeling peckish" << endl;
	break;
case hungry:
	cout << name << " is feeling hungry" << endl;
	break;
case starving:
	cout << name << " is starving" << endl;
	break;
case dead:
	cout << name << " has died because you did not feed him" << endl;
	break;

}

return currentState;

}

int feed() // function to feed pet
{
	cout << "Press F to feed " << name << endl;
	cin >> input;

	

	if (input == 'F')
	{

		switch (currentState) // changes state of pet after pet has been fed
		{
		case fed: cout << name << " is full and cannot be fed!" << endl;
			break;
		case peckish: cout << name << " has been fed!" << endl;
			currentState = fed;
			break;
		case hungry: cout << name << " has been fed!" << endl;
			currentState = peckish;
			break;
		case starving: cout << name << " has been fed!" << endl;
			currentState = hungry;
			break;
		case dead: cout << name << " is dead, you cannot feed him!" << endl;
			break;

		}

	}
	else

	{
		switch (currentState) // changes state of pet after pet has not been fed
		{
		case fed: cout << name << " has not been fed" << endl;
			currentState = peckish;
			break;
		case peckish: cout << name << " has not been fed" << endl;
			currentState = hungry;
			break;
		case hungry: cout << name << " has not been fed" << endl;
			currentState = starving;
			break;
		case starving: cout << name << " has not been fed" << endl;
			currentState = dead;
			break;
		case dead: cout << name << " is already dead" << endl;
			break;

		 
		}
	}
	return currentState;
}

int sleepState()
{
	switch (current)
	{
	case wide:
		cout << name << " is wide awake!" << endl;
		break;
	case awake:
		cout << name << " is awake" << endl;
		break;
	case tired:
		cout << name << " is feeling tired" << endl;
		break;
	case falling:
		cout << name << " is falling asleep!" << endl;
		break;
	case collapsed:
		cout << name << " has collapsed from lack of sleep!" << endl;
		break;
	}

	return current;
}

int sleep() // function to put pet to sleep
{
	cout << "Press S to make " << name << " have a nap" << endl;
	cin >> input;



	if (input == 'S')
	{

		switch (current) // changes state of pet after pet has been for a nap
		{
		case wide: cout << name << " is wide awake and can't sleep anymore!" << endl;
			break;
		case awake: cout << name << " has had a nap!" << endl;
			current = wide;
			break;
		case tired: cout << name << " has had a nap!" << endl;
			current = awake;
			break;
		case falling: cout << name << " has had a nap" << endl;
			current = tired;
			break;
		case collapsed: cout << name << " has collapsed because you didn't put him to sleep!" << endl;
			break;

		}

	}
	else
	{
		switch (current) // changes state of pet after pet has not been put to sleep
		{
		case wide: cout << name << " has not had a nap" << endl;
			current = awake;
			break;
		case awake: cout << name << " has not had a nap" << endl;
			current = tired;
			break;
		case tired: cout << name << " has not had a nap" << endl;
			current = falling;
			break;
		case falling: cout << name << " has not had a nap" << endl;
			current = collapsed;
			break;
		case collapsed: cout << name << " has already collapsed because he did not have a nap" << endl;
			break;


		}
	}
	return current;
}

int tiredhappylevel()
{
	switch (current)
	{
	case wide: tiredhappy = 4;
		break;
	case awake: tiredhappy = 3;
		break;
	case tired: tiredhappy = 2;
		break;
	case falling: tiredhappy = 1;
			break;
		case collapsed: tiredhappy = 0;
	}
	return tiredhappy;
}

int hungerhappylevel()
{
	switch (currentState)
	{
	case fed: hungerhappy = 4;
		break;
	case peckish: hungerhappy = 3;
		break;
	case hungry: hungerhappy = 2;
		break;
	case starving: hungerhappy = 1;
		break;
	case dead: hungerhappy = 0;
		break;
	}
	return hungerhappy;
}

int happiness()
{
	happinesslevel = hungerhappy + tiredhappy;
	switch (happinesslevel)
	{
	case 8: cout << "Extremely Happy!" << endl;
		break;
	case 7: cout << "Very Happy!" << endl;
		break;
	case 6: cout << "Happy" << endl;
		break;
	case 5: cout << "Feeling Ok" << endl;
		break;
	case 4: cout << "Quite Unhappy" << endl;
		break;
	case 3: cout << "Very Unhappy" << endl;
		break;
	case 2: cout << "Terribly Unhappy" << endl;
		break;
	case 1: cout << "Miserable" << endl;
		break;
	}
	return happinesslevel;
}

int update()
{
	switch (currentState)
	{
	case fed: cout << name << " is now feeling well fed!" << endl;
		break;
	case peckish: cout << name << " is still feeling peckish!" << endl;
		break;
	case hungry: cout << name << " is still hungry!" << endl;
		break;
	case starving: cout << name << " is now starving!" << endl;
		break;
	case dead: cout << name << " is now dead!" << endl;
		break;
	}
	switch (current)
	{
	case wide: cout << name << " is now feeling wide awake!" << endl;
		break;
	case awake: cout << name << " is now feeling awake but could still sleep a little more!" << endl;
		break;
	case tired: cout << name << " is still feeling tired and needs more sleep!" << endl;
		break;
	case falling: cout << name << " is still falling asleep and needs more sleep!" << endl;
		break;
	case collapsed: cout << name << " has collapsed because he did not get sleep!" << endl;
		break;

	}
	return 0;
}

int main()
{
	
				petname();
				petstate();
				feed();
				sleepState();
				sleep();
				happiness();
				update();
			system("pause");
}


Both the sleep & feeding work but the play option doesn't show for some reason? I just want the user to be able to press P, S or F at any time, not just when prompted!
Topic archived. No new replies allowed.