Decrease int value in member function

I am trying to build a dungeon crawler type game. I have figured out movement, and damage from attacks and traps. I have a variable 'health' that I initialize to 12. When you are attacked or step on a trap 1 point is decreased. This is working. I tried to return the health variable to main but it keeps being initialized to 12. How can I decrease health until it reaches 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
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
class movement
{
public:
	char wasd()
	{
		char mv;
		char up = 'w';
		char left = 'a';
		char right = 'd';
		char down = 's';

		cout << "\nMove using w for (up), a for (left), d for (right), and s for (down).\n\n";
		mv = _getch();

		if (mv == up)
		{
			cout << "You have moved up 1 space.\n";
		}
		else if (mv == left)
		{
			cout << "You have moved left 1 space.\n";
		}
		else if (mv == right)
		{
			cout << "You have moved right 1 space.\n";
		}
		else if (mv == down)
		{
			cout << "You have moved down 1 space.\n";
		}
		else
		{
			cout << "it didn't work.";

		}
		return 0;
	}
};

class random_enc
{	public:
	int treasure;
	int health = 12;
	public:
	int encounter(int)
	{
		int randnumber = rand() % 5 + 1;
		treasure = 0;
		if (randnumber == 1)
		{
			cout << "You have been attacked!!! You lose 1 hitpoint." << endl;
			health = --health;
			cout << "Hitpoints remaining: " << health << endl;
			return health;
		}
		else if (randnumber == 2)
		{
			cout << "You found treasure!" << endl;
			treasure = ++treasure;
			cout << "Treasure collected: " << treasure << endl;;
			return random_enc::treasure;
		}
		else if (randnumber == 3)
		{
			return health;
		}
		else if (randnumber == 4)
		{
			cout << "You step on a trap and take damage!! You lose 1 hit point.\n" << endl;
			health = --health;
			cout << "Hitpoints remaining: " << health << endl;
		}
		return health;
	}
};



int main()
{
	string name;

	cout << "Welcome to the dungeon, enter your name:\n";
	cin >> name;
	cout << "\nGood luck in the dungeon " << name << "\n";
	int health = 12;
	
	while (health != 0)
	{
		movement mvmt;
		random_enc random;
		mvmt.wasd();
		
		random.encounter(health);
	}
	cout << "You have been killed. Goodbye.\n";
	return 0;
}
Last edited on
First, this:
 
health = --health;
has undefined behavior. Either do this:
 
--health;
or this:
 
health = health - 1;

The same goes for the treasure = ++treasure line.

Second, the problem is that you're not using the return value correctly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int encounter() //Don't take an int. You're not using it for anything.
{
    //...
    return health;
}

//...

while (health != 0)
{
    movement mvmt;
    random_enc random;
    mvmt.wasd();
		
    health = random.encounter();
}
Last edited on
Thanks helios. I made those changes and that made my code cleaner. I made a change to the declaration of health in the class random_enc. I removed the initialization so it it just int health; This allows the return and decreases the number but the number starts at a large negative number:
1
2
3
4
5
Move using w for (up), a for (left), d for (right), and s for (down).

You have moved down 1 space.
You have been attacked!!! You lose 1 hitpoint.
Hitpoints remaining: -858993461
Who said you should remove the initialization?
No one but every time I go through the class 'random' It gets reinitialized at 'main'. When I removed it the number would decrease but did not start at 12.
It looks like you want to remove the local variable "health" from encounter and instead pass in main's "health" by reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

void encounter(int& health)  // note the ampersand
{
    --health; // this will change health in main
}

int main()
{
    int health = 12;
    encounter(health);
    cout << health << '\n';  // prints 11
}

Thanks Dutch that did the trick. Can you explain why it worked? I suspect that the function call just passed the pointer so that the value was not carried over. Then the value was determined dynamically in the function. What does it matter where the & is placed? Is 'int& health' the same as 'int &health'?
Last edited on
1
2
3
4
void encounter(int health)  // note the ampersand
{
    --health;
}


In this code, the value health is copied into the function. The local copy of health (in the function) is decremented, but the original value (where the function was called) is not changed. Only the copy of the value is changed.

1
2
3
4
void encounter(int& health)  // note the ampersand
{
    --health; // this will change health in main
}


In this code, health is a reference variable (denoted by the &), which means that it refers to the actual variable that was passed into the function. Any changes to health are made to the variable where the function was called so that changes within the function are visible outside the function.

'int& health' and 'int &health' are the same. There are religious wars about which is better style / correct.

There is also a middle way. int & health
Ah, a centrist.
@Ganado,
it was the Buddha who spoke first about the middle way.
Topic archived. No new replies allowed.