Function executing without being called

Hi, I'm currently teaching myself the basics of c++. I have the foundations for control statements, functions, classes, etc. and I decided I would test my skills through making a text based RPG. At current, I am writing code for the player to do stuff while in town, ex. go to the inn, go to the trader. Here is the code for my main function, town menu function, and the subsequent functions for activities in town. (trade, adventure, and map are purposely empty; I haven't written code for them yet.)

1
2
3
4
5
6
7
8
9
10
11
12
13
int main(void)
{
	using std::srand;
	using std::time;
	srand(time(0));

	if (menu() == 1)
	{
		player.CharGen();
		intro();
		townMenu();
	}
}


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
int townMenu(void)
{
	int town_choice;
	do 
	{
		cout << "You are in TOWN\n";
		cout << "1) Visit the local inn.\n"
			<< "2) Shop at the local trader.\n"
			<< "3) Head out towards a dungeon.\n"
			<< "4) View the map.\n";
		cin >> town_choice;
	} while (town_choice < 1 || town_choice > 4);
	switch (town_choice)
	{
	case 1:
		inn();
		break;
	case 2:
		trade();
		break;
	case 3:
		adventure();
		break;
	case 4: 
		map();
		break;
	}
	return 0;
}


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
void inn()
{
	string rumorArray[5] = {"Rumor 1.", "Rumor 2.", "Rumor 3.", "Rumor 4.", "Rumor 5."};
	int rumorChoice;
	rumorChoice = rand() % 4 + 0;
	int inn_choice; 
	while (true)
	{
	do
	{
		cout << "1) Buy a cup of ale. (5 gold)\n"
			<< "2) Ask about lore and legends from the bartender.\n"
			<< "3) Leave the inn.\n";
		cin >> inn_choice;
	} while (inn_choice < 1 || inn_choice > 3);
	switch (inn_choice)
	{
	case 1:
		if (player.gold > 0)
		{
		player.gold = player.gold - 5;
		player.drunk++;
		cout << "You down a hearty mug of ale.\n"
			<< "You now have " << player.gold << " gold.\n";
	    if (player.drunk >= 5)
		{
			cout << "You sway and swoon from drinking copious amounts of ale.\n"
		<< "You stumble away from the bar and crash into the street.\n"
		<< "You awake several hours later and several coins lighter.\n"
		<< "Do try to be more careful next time, brave hero...\n";
			player.drunk = 0;
			player.gold = player.gold - 25;
		}
		}
		else
			cout << "You haven't a pot to piss in.\n";
		break;
	case 2:
		cout << rumorArray[rumorChoice] << "\n";
		rumorChoice = rand() % 4 + 0;
		break;
	case 3:
		player.drunk = 0;
		townMenu();
		break;
	}
	}
}

void map()
{

}

void trade()
{
	cout << "You use the trade menu";
}

void adventure()
{

}


The issue I am having is that when I compile, I can use the inn function and do the subsequent actions that occur within the inn function, but then when I try to use another one of the functions within the town menu (go to the trader) the inn function executes again after the trade function executes. I would highly appreciate someone taking a look at this for me and providing some feedback.

Much thanks,
Tyler
You are trying to use functions as gotos. That is not really how they work. If townMenu is calling inn, then inn probably should not be calling townMenu.

When a function exits, flow returns to whatever code called it. Here, since inn was called by townMenu, when inn() exits, flow will return to townMenu. So there is no need for inn to call townMenu, it can just return/exit normally.


What's happening is this:
1) main calls townmenu
2) townmenu calls inn
3) inn calls townmenu[2] when the player decides to leave the inn
4) townmenu[2] calls trade
5) trade returns (flow goes back to townmenu[2])
6) townmenu[2] returns (flow goes back to inn)
7) inn resumes...
Disch, thanks for your reply. I removed the call to the townMenu() in case 3 of the switch in the inn() and replaced it with a return;
Now when it compiles, it just exits the program. My question now is, how can I get the inn function to flow back to the townMenu function?
Simple: Use a while loop in your main. When inn returns, also townmenu will return. So in main you will have to loop the townMenu function.
Thanks EssGeEich, it worked! Now I can continue coding the rest of the game; this issue had me frustrated for several days.
Don't worry too much about this. I'm posting it just to be informative. It's good to know, but you may not be ready for it yet:



Having a function for each location and calling them from each other this way may seem like a good idea, but it has some very serious problems, and intermediate/advanced programmers don't design games this way.

One problem is that it assumes that every location has one entry point, and must exit through that entry point. For a simple example, let's say that:
You can enter Town() from Overworld()
You can enter Inn() from Town()
You can enter Cellar() from Inn()
You can enter Town() from Cellar() (via some secret back exit or something).

How can you handle this problem? Cellar can't call Town() because if it does, when you exit the town you will go back to the cellar (instead of going back to the Overworld). Also, if the player decides to be cute and go town->inn->cellar->town->inn->cellar->etc, you will eventually consume all your stack space and the program will crash.



Another problem is that is makes you hardcode everything. Say for example you want a few commands that are common to every screen, such as inputting 'i' to check your inventory. If each location has its own code to run the menu, then you have to add this command to every single location. What if you have 20+ locations then decide you want to add a new common command? Or change the way a command works? That's a lot of code you have to change. It's also very easy to "miss" one and have your command broken, but only in one location.



Therefore, the typical way to do things is to write code once, and have that code apply to every location. The actual information about the location could be stored in variables, rather than being hardcoded into the game logic.

Getting this right with most genres is easy, as there is usually very little change in program flow through the game. But for heavily event-driven text adventure games, it's actually surprisingly difficult to design a data structure that can cover all game events in a common way. In a sense... this makes Text Adventure games harder to write than many other kinds of games, like a simple vertical shooter (Space Invaders clone or something).


I walked one guy through an example of how to design a dynamic text adventure in the below thread. Something like that is probably how most more-experienced programmers would approach the game. You might find it to be informative/interesting:

http://www.cplusplus.com/forum/beginner/71141/#msg379639

Last edited on
Thanks for your insight Disch. I am learning from only one book (c++ programming, Mark Lee) and it is a beginner book, therefore doesn't describe the most advanced/efficient programming practices. I try to use oop and efficient practices when I can, but I often find myself tempted to hardcode. I'll read your post and maybe postpone my text-adventure until I have more experience under my belt. Thanks again for being so helpful.
That's cool. I'm glad to be of help.

I didn't want to talk you out of doing anything you enjoy. If you have fun writing text adventure games this way.. then do it. That's how you get experience. No need to postpone anything.

But yeah like I said don't really worry about it. I was just throwing that out there because I thought it would give you some insight.
Topic archived. No new replies allowed.