Function Returns?!

Hi there relatively new C++ Programmer and during the construction of a Text-Based adventure games I decided to handle movement by hard coding a bunch of commands inside of each "Hall" function.

Once the command is fulfilled it compares it to all of the strings at run time, then returns the desired "Hall" function(Hall1(), Hall2()..etc.)but once a function is actually returned the console shuts down...without error.

Now I do not want this to happen, I want it to continue(I guess you could say 'restart') the function and allow the player to input commands to help them progress further(If the function called is the same) or transfer into the next hall if such a function is called.

Everything works properly except for the console abruptly shutting down on me once a function is returned...

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
#include<iostream>
#include<string>


using namespace std;

int Start();
int Inventory();
int Hall1();
int Hall2();
int Hall3();
 string Choice;


int (*foo)();

string HALL1STORY = "STORY1\n";
string HALL2STORY = "STORY2\n";
string HALL3STORY = "STORY3\n";

const int MAX_ITEMS = 10;
string inv[MAX_ITEMS];
int numItems = 0;




int main()
{
	int choice;

	foo = Hall1;
	
	

	
	cout << "\t\t++++++++++++++++++Weclome--------------------\n";
		cout << "To the great text adventure, this small adventure has you twisting and turning through the bleak corridors of the strange and seemingly ever changing metal building.";
		cout<< "Apon a hazey walking you quickly notice that you have been kidnapped, the  small cube like room taunts you as questions continue to fill your mind\n...Why was I taken, Who took me, Will I get out?\n\n";

	cout << "List of Commands: \n" ;
	cout << "MOVE -\n";
	cout << "EAST\n";
	cout << "WEST\n";
	cout << "NORTH\n";
	cout << "SOUTH\n\n";
	cout <<"------------\n";

	cout << "PICKUP [Item]\n";
	cout << "ATTACK\n";
	cout << "INVENTORY\n";
	cout << "USE [Item]\n";

	cout << "Use these words to navigate and interact with various parts of the story\n\n";
	
	cout <<"What would you like to do\n";
		cout<< "1) Play game\n";
			cout << "2) Quit\n";
		cin >> choice;

	switch(choice)
	{
	case 1:
		return Start();
		break;
		
	case 2:
		
	cin.ignore(10, '\n');
	cout << "PRESS ENTER TO QUIT";
	cin.ignore(10,'\n');
	cout << "GOODBYE";
		return 0;
	}
	}

int Start()
{

	string Choice;

	cout << HALL1STORY;
		getline(cin, Choice);
	
	return Hall1();

	}


	
//THE STARTING HALL FUNCTION
int Hall1()
{
	
	  char command[100];
	  char item[100];
	


	cout << "How would you like to procced?:\n";
			
			
		//MOVEMENT
	while(Choice.compare("MOVE SOUTH") != 0 && Choice.compare("MOVE EAST") != 0 && Choice.compare("MOVE NORTH") != 0 && Choice.compare("MOVE WEST") != 0 &&  Choice.compare("PICKUP") != 0 && Choice.compare("ATTACK") != 0 && Choice.compare("USE") != 0 && Choice.compare("PICKUP") && Choice.compare("USE DETONATOR") != 0 && Choice.compare("USE C4") != 0 && Choice.compare("USE SERVICE ELEVATOR") != 0 && Choice.compare("USE SECURITY DOOR") != 0 && Choice.compare("USE SECURITY CONSOLE") != 0 && Choice.compare("USE SECURITY PASS")  != 0 && Choice.compare("PICKUP") != 0 && Choice.compare("PICKUP SECURITY PASS") != 0 && Choice.compare("PICKUP C4") != 0 && Choice.compare("PICKUP DETONATOR") != 0 && Choice.compare("INVENTORY") != 0 && Choice.compare("1") != 0)
	{
		getline(cin,Choice);


		if(Choice.compare("MOVE SOUTH") != 0 && Choice.compare("MOVE EAST") != 0 && Choice.compare("MOVE NORTH") != 0 && Choice.compare("MOVE WEST") != 0 &&  Choice.compare("PICKUP") != 0 && Choice.compare("ATTACK") != 0 && Choice.compare("USE") != 0 && Choice.compare("PICKUP") && Choice.compare("USE DETONATOR") != 0 && Choice.compare("USE C4") != 0 && Choice.compare("USE SERVICE ELEVATOR") != 0 && Choice.compare("USE SECURITY DOOR") != 0 && Choice.compare("USE SECURITY CONSOLE") != 0 && Choice.compare("USE SECURITY PASS")  != 0 && Choice.compare("PICKUP") != 0 && Choice.compare("PICKUP SECURITY PASS") != 0 && Choice.compare("PICKUP C4") != 0 && Choice.compare("PICKUP DETONATOR") != 0 && Choice.compare("INVENTORY") != 0 && Choice.compare("1") != 0)
		{
		
			cout << "That is not a vaild command, please pick from the list : \n MOVE\nNORTH\nEAST\nSOUTH\nWEST\nPICKUP\nATTACK\nUSE\n";

		}
		if(Choice.compare("MOVE NORTH") == 0 )
		{

			cout << "A Solid steel wall blocks your path, you aren't going anywhere in that direction...\n";
			Hall1();
		
		

		}
		
		if(Choice.compare("MOVE WEST") == 0)
		{
		cout << "A Solid steel wall blocks your path, you aren't going anywhere in that direction...\n";
		Hall1();
		
		}

		if(Choice.compare("MOVE EAST") == 0)
		{
		cout << HALL2STORY;
		return Hall2();
		
		}

		if(Choice.compare("MOVE SOUTH") == 0)
		{
		cout << HALL3STORY;
		return Hall3();
		
		}
	//ITEMS

	//USE
		if(Choice.compare("USE") ==0 )
		{
			cout << "Pick an item or enviorment object to interact with.\n";
			Hall1();
		}
	//PICK UP
		if(Choice.compare("PICKUP") ==0)
		{
		cout << "Choose an item to pick up.\n";
		Hall1();
		}
	//USE ITEMS
		if(Choice.compare("USE DETONATOR") ==0 )
		{
		cout << "ITEM NOT AQUIRED\n";
		Hall1();
		}
		if(Choice.compare("USE C4") ==0 )
		{
		cout << "ITEM NOT AQUIRED\n";
		Hall1();
		}
	if(Choice.compare("USE SECURITY CARD") ==0 )
	{
		cout << "ITEM NOT AQUIRED\n";
		 Hall1();
	}
	//PICK UP ITEMS
	if(Choice.compare("PICKUP DETONATOR") ==0 )
	{
		cout << "ITEM NOT FOUND\n";
		Hall1();
	}
	if(Choice.compare("PICKUP C4") ==0 )
	{
		cout << "ITEM NOT FOUND\n";
		 Hall1();
	}
	if(Choice.compare("PICKUP SECURITY CARD") ==0 )
	{
		cout << "ITEM NOT FOUND\n";
		Hall1();
	}
	//ENVIROMENT OBJECTS
	if(Choice.compare("USE SECURITY CONSOLE") ==0 )
	{
		cout << "No console is around.\n";
		 Hall1();
	}
	if(Choice.compare("USE SECURITY DOOR") ==0 )
	{
		cout << "No Door is around.";
		 Hall1();
	}
	if(Choice.compare("USE SERIVCE ELEVATOR") ==0 )
	{
		cout << "No Elevators are around...";
		Hall1();
	}
	if(Choice.compare("INVENTORY") ==0 )
	{
		return Inventory();
	}


	}
}

  int Hall2()
	{
	// Hall2() Code(Identical template to Hall1());
}

  int Hall3()
  {
	  return 0;
  }
  
  int Inventory()
{

	int choice;
		cout << "Inventory:\n";
		
		for(int i = 0; i < numItems; ++i)
		{
			cout << inv[i] << endl << "\n";
		}

		cout << "1) Return to Game";
		cin >> choice;

	switch(choice)
	{
	case 1:
	return foo();
		
	}


}


The only function that does not instantly close on me is my "Inventory" function(I added a function pointer to track the hallways so the Inventory function could return to the previous hall when closed) but once the previous hallway function is called the console shuts down...

Any help is greatly appreciated!

Thanks.
You are using functions as if they were gotos. This is a bad idea.

Remember that when a function exits, program flow will continue at the point where the function was called:

1
2
3
4
5
6
7
8
9
10
11
void DoSomething()
{
  // do something here
}

int main()
{
  DoSomething();  // once DoSomething exits...

  cout << "...code continues to execute here";
}


You should not have Hall1 call Hall2 which calls Hall1 which calls Hall2 etc etc. The computer has to remember each of those calls so it can return back once the function exits. So you suck up more and more stack space, and if it's long enough, you can run out and crash the program. Not to mention it makes for sloppy and hard to follow code.


A simpler approach to this problem is the "switch on current location" technique:

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
enum Location
{
  Hall1,
  Hall2,
  Hall3,
  RestartGame
  ExitGame
};

Location DoHall1()
{
  // do Hall1 stuff here
  //  return a "Location" based on what action you want to take.
  //  for example if the user moves to hall 2, you'd do:
  return Hall2;
}

// make similar functions for Hall2, etc

void RunGame()
{
  Location loc = RestartGame;

  while(loc != ExitGame)  // as long as the game is still going...
  {
    if(loc == RestartGame)
    {
      // restart the game here
    }

    switch(loc)
    {
    case Hall1:  loc = DoHall1(); break;
    case Hall2:  loc = DoHall2(); break;
    //  etc
    }
  }
}



With this system, flow always returns immediately to RunGame, which calls the appropriate hall function.



There are of course, much better ways to do this, but in the interest of keeping this simple the above should work.
Topic archived. No new replies allowed.