class variables

Can anyone tell me why, when I set a private string variable in a class in one function. when i go to another function and use a getXXX() function to get that private variable it comes back empty.
We would need to see a code of your set and get to be able to tell you that
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
32
#ifndef PLAYER_H
#define PLAYER_H
#include <iostream>
using namespace std;

class Player
{
public:
	Player();
	string getUsername();
	void setUsername(string);
	double getDamage();
	void setDamage(double);
	double getHealth();
	void setHealth(double);
	int getLevel();
	void setLevel(int);
	double getLevelExperience();
	void setLevelExperience(double);
	double getCurrentExperience();
	void setCurrentExperience(double);
	int getXCord();
	int getYCord();
	void setXCord(int);
	void setYCord(int);
private:
	int level, xCord, yCord;
	double damage, health, levelExperience, currentExperience;
	string username;
};

#endif 

Player.cpp
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
#include "Player.h"

Player::Player(){
	level = 1;
	currentExperience = 0;
	levelExperience = ((level * 10) + (level * level));
	health = ((level * 20) + 60);
	damage = ((level * 4) + 8);
	xCord = 0;
	yCord = 0;
}

string Player::getUsername(){
	return username;
}

void Player::setUsername(string username){
	this->username = username;
}

double Player::getDamage(){
	return damage;
}

void Player::setDamage(double damage){
	this->damage = ((rand()%8)+damage);
}

double Player::getHealth(){
	return health;
}

void Player::setHealth(double health){
	this->health = health;
}

int Player::getLevel(){
	return level;
}

void Player::setLevel(int level){
	this->level = level;
}

double Player::getLevelExperience(){
	return levelExperience;
}

void Player::setLevelExperience(double levelExperience){
	this->levelExperience = levelExperience;
}

double Player::getCurrentExperience(){
	return currentExperience;
}

void Player::setCurrentExperience(double currentExperience){
	this->currentExperience = currentExperience;
}

int Player::getXCord(){
	return xCord;
}

int Player::getYCord(){
	return yCord;
}

void Player::setXCord(int xCord){
	this->xCord = xCord;
}

void Player::setYCord(int yCord){
	this->yCord = yCord;
}

Main.cpp
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
#include <iostream>
#include <string>
#include "Player.h"
#include "Opponent.h"

using namespace std;

void startFight();
void moveLocation();



int main()
{
	Player User;
	
	string name;

	cout << "Username: ";
	getline(cin, name, '\n');

	User.setUsername(name);

	moveLocation();

	char fight;
start:	
	cout << "\nWould you like to Fight? (y/n): ";
	cin >> fight;
	cin.ignore(100, '\n');
	
	if (toupper(fight) == 'Y')
	{
		startFight();
	}
	else if (toupper(fight) == 'N')
		exit(1);
	else
	{
		cout << "Error: Invalid input!" << endl;
		goto start;
	}


	return 0;
}


combat.cpp
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
#include "Player.h"
#include "Opponent.h"
#include <iostream>
#include <string>
using namespace std;

void getAttack(double);
void getRun();
double getExperience();
void setExperience(double);
void setLevel();

void moveLocation();


Player User;
Opponent Monst;


void startFight()
{

	char action;
	do{
		if (Monst.getHealth() > 0)
			cout << Monst.getName() << " has " << Monst.getHealth() << " health remaining!" << endl;
		if (User.getHealth() > 0)
			cout << User.getUsername() << " has " << User.getHealth() << " health remaining!" << endl;

		cout << "\nYou want to (A)ttack or (R)un: ";
		cin >> action;
		cin.ignore(100, '\n');
	}while ((toupper(action) != 'A')&&(toupper(action) != 'R'));

	if (toupper(action) == 'A')
		getAttack(getExperience());
	if (toupper(action) == 'R')
		getRun();
}

void getAttack(double experience)
{
	bool dead = 0;
	User.setDamage(10*User.getLevel());
	Monst.setDamage(10*Monst.getLevel());

	Monst.setHealth(Monst.getHealth() - User.getDamage());
	
	if (Monst.getHealth() > 0)
		cout << Monst.getName() << " hit " << User.getUsername() << " for " << Monst.getDamage() << " damage!" << endl;
	if (User.getHealth() > 0)
		cout << User.getUsername() << " hit " << Monst.getName() << " for " << User.getDamage() << " damage!" << endl;

	if (Monst.getHealth() <= 0)
	{
		cout << User.getUsername() << " has killed " << Monst.getName() << " and gained " << getExperience() << " exp!" << endl;
		setExperience(experience);
		dead = true;
	}
	if (Monst.getHealth() > 0)
		User.setHealth(User.getHealth() - Monst.getDamage());

	if (dead == true)
	{
		User.setHealth((User.getLevel() * 20) + 60);
		Monst.setHealth((Monst.getLevel() * 15) + 65);
		moveLocation();
	}
	if (dead == false)
		startFight();
}

void getRun()
{
	int getAway;

	getAway = (rand()%5);

	if (getAway == 2)
	{
		cout << User.getUsername() << " got hit for " << Monst.getDamage() << " damage and ";
		User.setHealth(User.getHealth() - Monst.getDamage());

		if (User.getHealth() > 0)
		{
			cout << "got away safely!" << endl;

			User.setHealth((User.getLevel() * 20) + 60);
			Monst.setHealth((Monst.getLevel() * 15) + 65);

			moveLocation();
		}
			
		if (User.getHealth() < 0)
		{
			cout << "got killed!" << endl;
			cout  << &Player::getUsername << " lost " << (User.getCurrentExperience() - (User.getCurrentExperience() * .1)) << " exp!" << endl;

			User.setHealth((User.getLevel() * 20) + 60);
			Monst.setHealth((Monst.getLevel() * 15) + 65);

			User.setCurrentExperience(User.getCurrentExperience() - (User.getCurrentExperience() * .1));

			moveLocation();
		}
	}
	else
	{
		cout << &Player::getUsername << " got away safely!" << endl;
	
		User.setHealth((User.getLevel() * 20) + 60);
		Monst.setHealth((Monst.getLevel() * 15) + 65);

		moveLocation();
	}
	cin.get();
}

double getExperience()
{
	double experience;

	experience = ((Monst.getLevel() / User.getLevel()) * User.getLevel()); 
	
	return experience;

}

void setExperience(double experience)
{
	User.setCurrentExperience(User.getCurrentExperience() + experience);
	setLevel();
}

void setLevel()
{
	if (User.getCurrentExperience() >= User.getLevelExperience())
	{
		if (User.getLevel() < 20)
		{
		User.setLevel(User.getLevel() + 1);
		User.setCurrentExperience(0);
		cout << User.getUsername() << " is now level " << User.getLevel() << "!" << endl;
		}
	}
}

Move.cpp
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
#include <iostream>
#include <string>
#include "Player.h"

using namespace std;

void showStats();
void saveGame();

void moveLocation()
{
	Player User;

	bool safeDirection, findCreature, setCoords = false;
	int xCord = 0, yCord = 0;
	int rndmCreature;
	char direction;

	do{
		do{
		rndmCreature = (rand()% 3);
		findCreature = false;
		safeDirection = true;
			

		if (setCoords == false)
		{
			User.setXCord(User.getXCord());
			User.setYCord(User.getYCord());
		}
		setCoords = true;

		
		cout << "(C)haracter to see your stats, s(A)ve your game, (L)oad saved game.\n";
		cout << "Where would you like to go? (N)orth, (E)ast, (S)outh, (W)est: ";
		cin >> direction;

		switch(toupper(direction))
		{
		case 'N':
			if (User.getXCord() > 0)
			{
				xCord = (xCord--);
				User.setXCord(xCord);
				
				if (rndmCreature == 1)
					findCreature = true;
				break;
			}
			else
			{
				cout << "You can not go further North.\n" << endl;
				safeDirection = false;
				break;
			}
		case 'E':
			if (User.getYCord() < 19)
			{
				yCord = (yCord++);
				User.setYCord(yCord);

				if (rndmCreature == 1)
					findCreature = true;
				break;
			}
			else
			{
				cout << "You can not go further East.\n" << endl;
				safeDirection = false;
				break;
			}
		case 'S':
			if (User.getXCord() < 19)
			{
				xCord = (xCord++);
				User.setXCord(xCord);

				if (rndmCreature == 1)
					findCreature = true;
				break;
			}
			else
			{
				cout << "You can not go further South.\n" << endl;
				safeDirection = false;
				break;
			}
		case 'W':
			if (User.getYCord() > 0)
			{
				yCord = (yCord--);
				User.setYCord(yCord);
				
				if (rndmCreature == 1)
					findCreature = true;
				break;
			}
			else
			{
				cout << "You can not go further West.\n" << endl;
				safeDirection = false;
				break;
			}
		case 'C':
			showStats();
		case 'A':
			saveGame();
		case 'L':
			//loadGame();
		default:
			cout << "Invalid input!\n" << endl;
			safeDirection = false;
		}
		}while (safeDirection == false);
	}while (findCreature == false);
}


Let me know if you need to see more of it. I have other .cpp's
You have a scope problem.

You have declared a Player user in your main() function, but you have also declared a global Player user in your combat.cpp as well as another local Player user in your movelocation function.

These are all separate variables. You have three separate variables in your program called user, and this is possible because they are all in different scopes.

The easiest, though not exactly proper, way to resolve this is:
1. Move your global declarations of Player and Opponent out of combat.cpp and into main.cpp (before your main() function).
2. Delete the local declarations in main() and movelocation().

Using globals generally isn't recommended, but I wouldn't worry about that for now.

Using goto though, is highly discouraged at all levels. Replace it with a proper looping structure, either while(), or do/while().
So you mean delete all my local variables out of main() and moveLocation() or just move them out to make them global.
No, instead you should delete your global and pass the Player object through reference
so declare my player object in my main.cpp then pass it like this; moveLocation(&Player); ? or if that's doing it wrong could you show me an example.
That's right, only the & comes after the type, not before
Thanks mate that helped alot!
Topic archived. No new replies allowed.