Variable problem

So I have a variable called curPlayerHealth and another called maxPlayerHealth. So I need the curPlayerHealth to be set to maxPlayerHealth, ONLY ONCE.

At the moment everytime I call the function it resets the health when I actually just want it to find the current health.

The function is:

1
2
3
4
5
6
7
8
int currentPlayerHealth()
	{	
                curPlayerHealth = maxPlayerHealth;
		cout << "Current health: " << curPlayerHealth << endl;
		cout << endl;

		return curPlayerHealth;
	}


Thanks!
Last edited on
You might want to reconsider your design. The function returns the current players health, but you start with an intialization. This should be done somewhere else. The only thing this function should do is return and display the current health quantifier.
1
2
3
4
5
	int setCurPlayerHealth()
	{
	curPlayerHealth = maxPlayerHealth;
	return curPlayerHealth;
	}


That's still not working. curPlayerHealth and maxPlayerHealth are both global.

The only reason why I made it a function is because maxPlayerHealth is set in a function before the currentPlayerHealth. Therefore since I'm setting curPlayerHealth to maxPlayerHealth, I need to set the variable before the currentPlayerHealth and after the function PlayerHealth() where maxPlayerHealth is defined.
Last edited on
Any help please?
Why not set current health equivent to max health in the same function where it's first defined? I mean, I don't understand why you need to set player health to max every time you want to query player health.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int currentPlayerHealth(int curPlayerHealth)
{	
  cout << "Current health: " << curPlayerHealth << endl;
  cout << endl;

  return curPlayerHealth; // not really needed since its value is the same
                                        // as when the function was called
}

int main()
{
  int maxPlayerHealth = 100;
  int curPlayerHealth = maxPlayerHealth;
  curPlayerHealth -= 30;
  currentPlayerHealth(curPlayerHealth);
  cin.get(); // to prevent the console from closing 

How is this?
The problem is, my maxPlayerHealth is randomly generated inside it's own function. The point is, it's random and so therefore I can't set it. Probably would have been better to mention that before :P
Last edited on
Then just set curPlayerHealth to the random value right after it is generated. Also try to avoid using global variables. Pass locals as arguments
Can't believe I didn't think of this.. Thanks!
Lol
Topic archived. No new replies allowed.