Question about return statements

So, I'm writing a text adventure, and in it I the user having an inventory. Now, instead of writing the same code every time the user wants to check their inventory, I decided to make a function for it, and just have the program call that function every time they press 'I'. My question is, is there any way to make the function return to whichever function called it?

I can do my best to elaborate if needed.
Yes. It's called a void function e.g
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void noReturn()
{
   std::cout <<  "This function is called but it does not return any values" << std::endl;
   //optional iirc, you can also just end the function here without return;
   //which means that it returns nothing ;)
   return;
}

int main()
{
   //call your void function
   noReturn();
   return 0;//optional too
}
By default the function returns to which ever one called it. Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int a()
{int A = b() + 1;
 return A;}

int b()
{int B = c() + 1;
 return B;}

int c()
{return 5;}

int main()
{
   std::cout << a();

return 0;}

This "app" will output 7 then exit.
Last edited on
I am writing a text based adventure right now if you want i can post the inventory and how to use it here when it is done.

here is what 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
//includes, librarys, namespace
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string>
using namespace std;

//character definition, characters, chars, char, player, npc, enemy
//#1
//line1: if it can be attacked it counts as a character
struct character
{
	//#2
	//line1: the combat system works like this the attack has a (*agility*25)%c chance of hitting making the max agility lvl 4. then if it hits the damage is 
	//line2: see comment #5"(attack lvl give or take a 3rd) - (defence lvl give or take a third)"
	int defence;
	int attack;
	int agility;
	int gold;
	int lvl;
	int xp;
	int health;
	string name;
};

//drops, drop, xp, eperience, gold, rewards
//#3
//line1: determines how much xp and hw much gold the enemy drops if killed
struct drop
{
	int gold;
    int xp;
};
//combat, fight, kill, die, win, death
		drop get_damage(character attacker, character defender)
		{
			//#4
			//line1: makes sure the attacker hits before continuing
			srand(time(0));
			if (rand() % 100 > attacker.agility * 25)
			{
			//#5
			//line1: gets the numbers referenced in comment #2 line2 
			//line2: dont look to hard at the rest unless you have to or your eyes may bleed
		    
			//the
			
			int def_ofset_down = defender.defence - (defender.defence / 3);
			int att_ofset_down = attacker.attack - (attacker.attack / 3);
			int def_ofset_up = defender.defence + (defender.defence / 3);
			int att_ofset_up = attacker.attack + (attacker.attack / 3);
			
			//rest
			srand(time(0));
			if(rand() % 150 > attacker.agility * 10)
			{
			//#6
			//line1: see comment #1 line1 for explaination on how combat works
			double hit = rand() % (att_ofset_up - att_ofset_down - 1) + att_ofset_down + 1;
			double sponge = rand() % (def_ofset_up - def_ofset_down - 1) + def_ofset_down + 1;
			int damage = hit - sponge;
			defender.health = defender.health - damage;
			cout << attacker.name << " hits " << defender.name << " for " << damage << " damage" << endl;
			cout << defender.name << " has " << defender.health << " health left" << endl;
			}
			else
			{
				cout << attacker.name << " attacks " << defender.name << " but " << defender.name << " deflects it" << endl;
			}
			}
		}

working on inventory right now.
Last edited on
Regarding your combat, I think you should simplify it abit. For example why doesn't def_ofset_down = defender.defence * .66; it's a little easier to follow that way and accomplishes almost the same thing. By the way, why have a total value for the attribute at all if you're only using two thirds of it?

The thing that usually inspires me are pencil and paper RPG's or complex board games. Don't copy the methods they use directly but read two or three of them then decide what you do and don't like, then mix and mash.
Sorry it took me so long to get back to you guys. I'm still having trouble getting the inventory function to return to whatever function called it. I tried void, and that just exits the program.

And bboy212, sure! I'd love to see how you wrote your inventory! Thanks!
Show us your code. The error must be somewhere else.
Alright. I'm a beginner...obviously, since I'm posting in this forum haha. So if anyone notices a more efficient way to do something, it would be much appreciated if you'd tell me how to do it.
Thanks!

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
#include "stdafx.h"
#include "iostream"
#include "string"
#include "cmath"
using std::cout;
using std::cin;
using std::endl;
using std::string;

int mainmenu();
int main();
int getname();
int firstroom();
int opendoor1();
bool holdkey1 = false;
void inventory();
string name;

int mainmenu()
{
	cout << "Welcome to <NAME HERE>!" << endl;
	cout << "Please select an option:" << endl;
	cout << "------------------------" << endl;
	int choice = 0;
	cout << "1. Play" << endl << "2. About" << endl << "3. Exit" << endl;
	cin >> choice;

	switch(choice)
	{
	// Starts the game
	case 1:
		return 0;
		break;
	// Informs the user about the game
	case 2:
		char done;
		cout << endl << "Welcome to my text-based adventure game. It is a very basic game, which will be updated as I learn more C++." << endl
			<< "Your objective is obviously to reach the end of the game." << endl
			<< "Death and exiting the program will cause you to have to start from the beginning" << endl
			<< "Controls:" << endl
			<< "---------" << endl
			<< "Movement - type [N]orth, [S]outh, [E]ast, or [W]est" << endl
			<< "Open things (doors, drawers) - type [O]pen" << endl
			<< "Take item - type [G]rab" << endl
			<< "Check Inventory - type [I]nventory" << endl
			<< "Enter any letter or number and hit enter to continue..." << endl;
		cin >> done;
		cout << endl;
		return mainmenu();
	// Closes the program
	case 3:
		exit(0);
	}

	return 0;
}

int main()
{
	// Brings the user to the main menu
	mainmenu();
	// Starts if the user typed in "1" in the mainmenu() function
	system("cls");
	cout << "Let's do this." << endl;
	getname();

	int f;
	cin >> f;
	return 0;
}

int getname()
{
	cout << "What is your name?" << endl;
	cin >> name;
	firstroom();
	
	return 0;
}

int firstroom()
{
	char choice1;
	cout << endl << "You are in a room, in front of you, you see a door. What would you like to do?" << endl;
	cin >> choice1;

	switch(choice1)
	{
	case 'O':
	case 'o':
		if (holdkey1 == true)
		{
			opendoor1();
		}
		else 
		{
			cout << "The door is locked." << endl;
			return firstroom();
		}
	case 'E':
	case 'e':
		cout << "You see a wall with a painting on it. You examine the painting, but find nothing. You return to your original position." << endl;
		return firstroom();
	case 'W':
	case 'w':
		char keyyn;
		cout << "You see a table with a key on it. Would you like to grab it? (Y or N)" << endl;
		cin >> keyyn;
		switch(keyyn)
		{
		case 'y':
		case 'Y':
			holdkey1 = true;
			cout << "You grab the key." << endl;
		case 'n':
		case 'N':
			return firstroom();
		}
	case 'S':
	case 's':
		cout << "Theres nothing behind you." << endl;
		return firstroom();
	case 'I':
	case 'i':
		inventory();
	}
	return 0;
}

int opendoor1()
{
	char choice2;
	cout << "You open the door. You are now outside, and in front of you, you see a house. To your left you see a path, and to your right you see a large building." << endl
		<< "Where would you like to go?" << endl;
	cin >> choice2;
	return 0;
}

void inventory()
{
	cout << "Currently, in your inventory, you have: " << endl;
	if (holdkey1 == true)
	{
		cout << "A key!" << endl;
	}
	return;
}

Your code makes it very obvious you are a beginner. It occurs to me you didn't quite grasp the idea behind functions and return types and stuff yet.
Well is there any chance you'd like to point me in the right direction to learn this stuff correctly?
closed account (D80DSL3A)
About your function returns.

1) Many of your functions could be of type void since the return values are not being used.
Example: int getname() defined starting on line 72 returns an integer value. Normally you make a function return a value because you need it to return the result of a calculation. As in this case:
1
2
3
4
5
6
// a function to add 2 numbers and return the result
int sum(int a, int b)
{
    int total = a + b;
    return total;
}

You would use this function like so:
1
2
int y;
y = sum(3,4);// the function will return the value 7 and assign it to y. 


You are calling getName() on line 65 but making no use of the return value. Does that make sense? The function may as well be void getName();

2) The BIG problem!
When you call the function itself in the return this is recursive function calling.
Example:
1
2
3
4
5
int firstroom()
{
    // some code
    return firstroom();// here the function calls itself, which calls itself...forever!
}


If you want the function to return to where it was called from, that's what return does.
1
2
3
4
5
void firstroom()
{
    // some code
    return;// here the function returns to where it was called in the program.
}


Hope that helps!
I would recommend you study how functions work a bit more before jumping into writing a game. Otherwise you are sentencing yourself to headaches like this and many more!
Just plod through the "boring" examples and exercises in the tutorials. You gotta know this stuff before you can use it well.
Try this sites tutorials on the subject:
http://www.cplusplus.com/doc/tutorial/functions/
or get a book on beginning c++ programming.
Haha yeah, I was thinking about that right after I posted on this forum, but I figured, if I can get some help, maybe I'll learn some things along the way. But I think I'll put it to the side until I get a little more advanced in C++ haha.

Thanks for the link on this site, though! I can't believe I didn't notice it before. I figured I'd start from the beginning though, since it doesn't hurt to re-learn things, and I ended up learning some new things already. Thanks again! :)
Last edited on
stupid schools been slowing down my coding but here is what I'm going to do when I get around to it: make a class that has these values: equipped, equipable, head equipable(and many more values like to to determine where you can put it), usable, weapon, damage, defense and so on. and functions to find out what it is called how much damage it has and different types of spells that can be used if the player has the right amount of manna that will affect stats. and of course one function that takes a string and sets values from that. it would be something like this:

void wat_is_it(string name)
{
objectname = name
if (name = item one)
//set stats to those of item one and so on.
}

sorry if this is messy i hope this makes sense.

*edit you could make it return bool. one if it finds an object and zero if it does not and you have to define it yourself.
Last edited on
1
2
3
4
5
6
void wat_is_it(string name)
{
objectname = name
if (name = item one)
//set stats to those of item one and so on.
}


Only makes sense when you have a global database.
Oh I forgot to say you would make an array of type inventory so you could just say:

inventory inv [10]
//at some point in the program set the stats
then you could just have a for loop circling through the array printing the names with a number before it that you could hit to see its stats.

@hanst99 is there a better way to set stats with only one variable being passed to the function?
course the wat_is_it function would be rather long.

*edit
Don't know if you'v ever played Dwarf fortress but the stats for EVERYTHING are in text files so you could do something like that if you wanted which would actually allow for some modablity in the game.
Last edited on
I would write an ItemDatabase class that can load a text file to create the items, and has a member method that takes an item name (or an unique numerical identifier - that would probably be better) - which it uses to identify an item and return the according Item object. It would also have a parameterless member method that would return a vector with all the item identifiers.
wow that is an awesome idea ty.
@computergeek01 I don's just use 2/3'ds of it I also have a def_offset_up which is def + 2/3'ds and I generate a random number between the two in order to make the total value that is subtracted from the hit value I believe I called it sponge because it soaks it up.
instead of
1
2
3
4
using std::cout;
using std::cin;
using std::endl;
using std::string;

just use
using namespace std;
it will include everything from std so you don't need to do it manually.
as for the functions it looks like you don't quite understand return values so check this out: http://www.cplusplus.com/doc/tutorial/functions/
also I noticed that in your inventory function it does not need to return anything. just make the function of type : void
Topic archived. No new replies allowed.