Getter Function Not Being Updated

closed account (N36fSL3A)
Hello!

Fredbill30 here! I have just joined these forums, but thank you guys for helping me with my previous problems with C++. Anyway, lets get started:

player.h
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
#ifndef _PLAYER_H_
#define _PLAYER_H_

class Player
{
	private:
		int health;
		int mana;
		int exp;
		int level;

	public:
		//Getter functions
		int getHealth()const {return health;}
		int getMana()const {return mana;}
		int getExp()const {return exp;}
		int getLevel()const {return level;}

		//Setter functions
		void setHealth(int h) {h = health;}
		void setMana(int m) {m = mana;}
		void setExp(int e) {e = exp;}
		void setLevel(int l){l = level;}



		//Normal Functions
		void player();
}player;

#endif _PLAYER_H_ 


main.h
1
2
3
4
5
6
7
8
9
10
11
/****************************************************************
*
*   * This, the main class, handles general things
*
****************************************************************/
// Subsitute Variables for Class Player
int h;
int m;
int e;
int l;
//=================================Break===================================// 


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

#include "main.h"
#include "player.h"



using namespace std;

int main()
{
	cout << "Input the value of your health : ";
	cin >> h;
	player.setHealth(h);
	cout << "\n The value of your health is ";
	cout << player.getHealth();
return 0;
}

If you compile this, you will see that this code has a serious problem: The getter function won't update, therefor I do not get the actual value entered by the user. I don't know what's wrong, can you guys please help me?
Last edited on by Fredbill30
You changed the value of h to the value of health
this is how it should be done
void setHealth(int h) {health = h;}
you should correct all setter functions
Last edited on
closed account (N36fSL3A)
Thanks Maniax!
Topic archived. No new replies allowed.