Function to alter a variable value and then return to the function?

So for a project, I'm creating an adventure game. One of the requirements is at the start of every room, it needs to display how much oxygen is left. I was initially going to do it with a game wide variable that would set itself at the beginning of every function, with oxygen = oxygen - 5, but the oxygen needs to be in a separate function, as per the requirements. I don't really understand how to move back and forth between functions very well, especially ones that are supposed to print a value at the start of every single room.

Here is my code 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
  /*
* February 17, 2018
* Assignment 04
* Section 02
*/

#include <iostream>
#include <stdlib.h> //This is the library for seeding the srand()
using namespace std;

//Function Prototypes
int menu();
//int oxygen();
int room1();
int room2();
int room3();
int room4();
int room5();
int room6();
int gameover();
int gamewin();

//Game-Wide Variables
int oxygen = 120;


//Main Function
int main()
{
	menu();
}

//Menu for play or exit
int menu()
{
	int menuchoice;
	cout << "Welcome to Asylum! Would you like to play?\n1 - Play\n2 - Exit\n";
	cin >> menuchoice;

	switch (menuchoice)
	{
	case 1: 
		room1();
		break;
	case 2: 
		return 0;
		break;
	default:
		cout << "Invalid Choice.\n\n";
		menu();
	}
}

//Oxygen Function
/*int oxygen()
{

}
*/

//Functions for rooms. The various text passages are separated into multiple cout statements so that it is easier to read for the programmer.

//Function Room 1
int room1()
{
	int room1choice;

	cout << "\n********** Oxygen Remaining: " << oxygen << " **********\n";
	cout << "Welcome to the Asylum.\n";
	cout << "You see a door on your right and a door on your left.\n";
	cout << "The door on your left has a cool breeze blowing from it. The door on your right smells of musty water.\n\n";
	cout << "Which door would you like to go through?\n";
	cout << "1 - Right\n2 - Left\n";
	cin >> room1choice;

	switch (room1choice)
	{
	case 1: room2();
		break;
	case 2: room3();
		break;
	default:
		cout << "Inavlid Choice.\n\n";
		room1();
	}



	return 0;
}

//Function Room 2
int room2()
{
	int room2choice;
	oxygen = oxygen - 5;
	cout << "\n********** Oxygen Remaining: " << oxygen << " **********\n";
	cout << "You hear a strange voice calling to you...in the corner, there is a box with a red box, yellow triangle, and blue square on it.\n";
	cout << "Which one do you press?\n";
	cout << "1 - Red Box\n2 - Yellow Triangle\n3 - Blue Square\n";
	cin >> room2choice;

	switch (room2choice)
	{
	case 1: 
		cout << "\nA hidden door opens in the wall, revealing a new path.\n";
		room3();
		break;
	case 2: 
		cout << "\nA hole in the ceiling opens above you, and a large bucket of acid comes pouring down on your head, killing you instantly.\n";
		gameover();
		break;
	case 3:
		cout << "\nA trap door opens beneath you, and you are surprised to find yourself back where you began!\n\n";
		room1();
		break;
	default:
		cout << "Inavlid Choice.\n\n";
		room2();
	}

	return 0;
}

//Function Room 3
int room3()
{
	int room3choice;
	cout << "\nThere is a blood spattered hospital bed in the corner of a room. Next to it is a partially open closet door.\n";
	cout << "Do you approach the bed, open the closet, or leave through the next door?\n";
	cout << "1 - Approach the Bed\n2 - Open the Closet\n3 - Leave\n";
	cin >> room3choice;

	switch (room3choice)
	{
	case 1:
		cout << "\nA horrendous monster is crouched behind the bed. It slowly stands up and grabs you, tearing you apart, as your screams echo throughout the chambers.";
		gameover();
		break;
	case 2:
		cout << "You open the closet door and step through it...\n\n";
		room5();
		break;
	case 3:
		room4();
		break;
	default:
		cout << "Inavlid Choice.\n\n";
		room3();
	}

	return 0;
}

//Function Room 4
int room4()
{
	int room4choice;
	cout << "You find yourself in a large courtyard area, with an old, rusty basketball court. You see a ball on the ground.\n";
	cout << "Do you go for the slam dunk, the three pointer, or the half court shot?\n";
	cout << "1 - Slam Dunk\n2 - Three Pointer\n3 - Half Court\n";
	cin >> room4choice;

	switch (room4choice)
	{
	case 1:
		cout << "\nYou soar towards the hoop, flying ever so gracefully, with not a care in the world!\n";
		cout << "The ball slams through the rim, through the hoop, as you come crashing in, grabbing the rim.\n";
		cout << "Out of nowhere, hundreds of NBA recruiters are all over you, desparately wanting you to play for them.\n";
		cout << "They take you out of the Asylum, and fly you away.\n";
		gamewin();
		break;
	case 2:
		cout << "\nYou shoot from the three point line, but miss horribly. You feel bad about yourself, and leave.";
		room5();
		break;
	case 3:
		cout << "\nWhat are you, a middle schooler? That's not cool anymore.\n";
		cout << "The ball flies into the rim, bounces back, and hits you in the head, killing you instantly.\n";
		gameover();
		break;
	default:
		cout << "Invalid Choice\n\n";
		room4();
	}
	return 0;
}

//Function Room 5
int room5()
{
	int room5choice;
	cout << "\nYou are now in an existential crisis. There's a computer in front of you with the Asylum game. Do you play it?\n";
	cout << "1 - Play\n2 - Leave\n";
	cin >> room5choice;

	switch (room5choice)
	{
	case 1:
		room1();
		break;
	case 2:
		room3();
	default:
		cout << "Inavlid Choice\n\n";
		room5();
	}
	return 0;
}

//Function Room 6
int room6()
{
	int room6choice;
	cout << "You're outside now, by a large gate. It has spikes on the top, and it is locked tight.\n";
	cout << "You notice a rocket launcher nearby. Do you shoot the rocket launcher at the gate, attempt to jump it, or head to the courtyard?\n";
	cout << "1 - Shoot the rocket launcher\n2 - Jump the gate\n3 - Head to the courtyard\n";
	cin >> room6choice;

	switch (room6choice)
	{
	case 1:
		cout << "You pull the trigger, but the launcher malfunctions, exploding and killing you instantly.\n";
		gameover();
		break;
	case 2:
		cout << "You attempt to jump over the gate, but you aren't as athletic as you thought. You are impaled and die instantly.\n";
		gameover();
		break;
	case 3:
		room4();
		break;
	default:
		cout << "Invalid Choice\n\n";
		room6();
	}
	return 0;
}

//Function Game Over
int gameover()
{
	int gameoverchoice;
	cout << "\nGame Over";
	cout << "\nWould you like to play again?";
	cout << "\n1 - Play Again\n2 - Exit\n";
	cin >> gameoverchoice;

	switch (gameoverchoice)
	{
	case 1: room1();
		break;
	case 2: 
		return 0;
		break;
	default:
		cout << "Invalid Choice\n\n";
		gameover();
	}

	return 0;
}

//Function Game Win
int gamewin()
{
	int gamewinchoice;
	cout << "Congratulations! You escaped!\n";
	cout << "\nWould you like to play again?";
	cout << "\n1 - Play Again\n2 - Exit\n";
	cin >> gamewinchoice;

	switch (gamewinchoice)
	{
	case 1: room1();
		break;
	case 2: 
		return 0;
		break;
	default:
		cout << "Invalid Choice\n\n";
		gamewin();
	}
}
Last edited on
like this?

void foo(int &value)
{
value--;
}

...

cout << reavalue << endl;
foo(realvalue);
cout << reavalue << endl;

or, if I understood your explain correctly, this might be what you need:

int o2handler(int modify)
{
static int o2 = initial value;
o2 += modify; //you can add or subtract by using a negative value.
return o2;
}

cout << " current o2 is " << o2handler(0) << endl; //zero does nothing!


you can also do all the above in a class, or a singleton class, or other various approaches. This is the straightforward simple approach. It more or less is a heavily controlled global variable, but if you are careless with this technique you can still inherit some of the malfunctions those cause. You REALLY don't want to multi-thread something like that, for one example, esp for a more complex task.

Last edited on
Topic archived. No new replies allowed.