Cannot Access Data Of Struct

When I try to access my struct elements in another class function besides the one that modified it, it shows as empty:

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
class Player
{
public:
	struct Account
	{
		int Position;
		string Name;
		int Score;
		string Date;
		int Level;
	};
	vector<Account> User;

};

void Player::LoadPlayerStats()
{
	// Push back User a bunch of times
	// Populate the elements of each struct
	
	// OUTPUT GIVES ME CORRECT VALUES
}

string Player::GetPlayerStats(int index)
{
	// OUTPUT GIVES ME NOTHING
	return User[index].Name;
}

int main()
{
	Player player;
	player.LoadPlayerStats();
	string str=player.GetPlayerStats(0);
	// OUTPUT GIVES ME NOTHING
	return 0;
}



I also try to access it directly like so:
1
2
3
4
5
6
int main()
{
Player player;
player.LoadPlayerStats();
cout<<player.User[0].Name; // Nothing outputs
}


Any help will be greatly appreciated!
Last edited on
Show definition of Player::LoadPlayerStats()
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
class Player
{
public:
	void LoadPlayerStats();

};

void Player::LoadPlayerStats()
{
	// Declaration's / Reset's
	File.open("Player Data.txt");
	FileVar = "";
	Counter = 0;
	User.clear();
	string strTemp = "";

	// Extract data from file
	while (File >> FileVar) //(!InFile.eof())
	{
		Counter++;
		if (Counter == 1)
		{
			User.push_back(Account());
			User[User.size() - 1].Name = FileVar;
		}
		if (Counter == 2)
		{ 
			User[User.size() - 1].Score = stoi(FileVar);
		}
		if (Counter == 3)
		{
			User[User.size() - 1].Level = stoi(FileVar);
		}
		if (Counter == 4)
		{
			strTemp = "";
			Dates.push_back(Date());
			for (int i = 0; i < 4; i++)
			{
				if (i == 0)
					{ Dates[Dates.size() - 1].DayOfTheWeek = FileVar; }
				if (i == 1)
					{ Dates[Dates.size() - 1].Month = FileVar; }
				if (i == 2)
					{ Dates[Dates.size() - 1].DayOfTheMonth = stoi(FileVar); }
				if (i == 3)
				{
					stringstream ss;
					ss << FileVar[0] << FileVar[1] << FileVar[3] << FileVar[4] << FileVar[6] << FileVar[7];
					Dates[Dates.size() - 1].Time = stoi(ss.str());
				}

				strTemp += FileVar;
				strTemp += " ";
				File >> FileVar;
			}
			Dates[Dates.size() - 1].Year = stoi(FileVar);
			strTemp += FileVar;
			strTemp += " ";

			User[User.size() - 1].Date = strTemp;
			Counter = 0;
		}
	}

	sort(User.begin(), User.end(), Sort_Scores_Descending());

	// Populate the positions vector
	for (int count = 0; count < User.size(); count++)
		{ User[count].Position = count + 1; }

	File.close();
}
Last edited on
Add check after loop and before sort: if( !File.eof() ) std::cerr << "an error occured reading file";
Check if it is output or not. Also you need to call clear on file stream before opening if you use persistent file and do not use C++11.

Can you post a minimal compiling example reproducing your problem? http://sscce.org/
I figured it out. It was a function that I didn't list here.
Topic archived. No new replies allowed.