Array in STRUCT does not get filled

Write your question here.
Why doesn't the scores array from the STRUCT get filled when adding user info?

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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
*	Chapter 11, Problem 4
*
*	Write a program that allows a user to enter high scores of a game,
*	keeping tracking of the name of the user and the score.  Add the
*	ability to show the highest score of each user, all scores for a
*	particular user, all scores from all users, and list of users.
*
*	Display menu:
*		0. Exit
*		1. Add user info
*		2. Display high scores of all users
*		3. Display all scores for a particular user
*		4. Display list of users
*	Write functions for each option
*
*/

#include <iostream>
#include <string>

using namespace std;


struct GameScores
{
	string name;
	int highest;
	int scores[5];
	int index_scores;
};

//	Function Prototype
GameScores AddUser(GameScores user[], int index, int numS);
void DisplayHighestScoreAllUsers(GameScores user[], int index);
void DisplayAllScoresOfUser(GameScores user[], int index);
void DisplayListOfUsers(GameScores user[], int index);

int main()
{
	//	Declare and initialize variables
	int choice;
	GameScores player[10];
	int countPlayers = 0;
	int numScores = 0;
	int index_player = 0;

	// Initialize score array
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 5; j++)
		{
			player[i].scores[j] = 0;
		}
	}

	while (true)
	{
		//	Display menu
		cout << "Menu: " << endl <<
			'\t' << "0. Exit" << endl <<
			'\t' << "1. Add user info" << endl <<
			'\t' << "2. Display highest scores of all users" << endl <<
			'\t' << "3. Display all scores of a particular user" << endl <<
			'\t' << "4. Display list of users" << endl << endl;

		cout << "Please make a selection: ";
		cin >> choice;

		switch (choice)
		{
		case 0:
			return 0;
		case 1:
			cout << "How many scores to enter? ";
			cin >> numScores;
			player[countPlayers] = AddUser(player, countPlayers, numScores);
			countPlayers++;
			cout << endl;
			break;
		case 2:
			DisplayHighestScoreAllUsers(player, countPlayers);
			cout << endl;
			break;
		case 3:
			cout << "The database contains players 0 to " << countPlayers - 1 << endl; 
			cout << ".  Which player do you want?: ";
			cin >> index_player;
			DisplayAllScoresOfUser(player, index_player);
			cout << endl;
			break;
		case 4:
			DisplayListOfUsers(player, countPlayers);
			cout << endl;
			break;
		}
	}
}


GameScores AddUser(GameScores user[], int index, int numS)
{
	cout << "Please enter player's name: ";
	getline(cin >> ws, user[index].name);
	
	cout << "Please enter highest score: ";
	cin >> user[index].highest;

	user[index].index_scores = numS;
	int score = 0;

	for (int i = 0; i < numS; i++)
	{
		cout << "Enter score " << i << ": ";
		cin >> score;
		user[index].scores[i] = score;
	}

	cout << user[index].name << endl;;
	cout << user[index].highest << endl;
	for (int i = 0; i < user[index].index_scores; i++)
	{
		cout << user[index].scores << endl;
	}

	return user[index];
}


void DisplayHighestScoreAllUsers(GameScores user[], int index)
{
	cout << "index" << index << endl;

	for (int i = 0; i < user[index].index_scores; i++)
	{
		cout << user[index].name << "score(s) are " << user[index].scores[i] << endl;
	}
}


void DisplayAllScoresOfUser(GameScores user[], int index)
{
	cout << "Print index_scores: " << user[index].index_scores << endl;
	cout << "Player's name: " << user[index].name << endl;
	cout << "Player's highest score: " << user[index].highest << endl;
	for (int i = 0; i < user[index].index_scores; i++)
	{
		cout << "score " << i << " = " << user[index].scores << endl;
	}
}


void DisplayListOfUsers(GameScores user[], int index)
{
	for (int i = 0; i < index; i++)
	{
		cout << "Player[" << i << "] is " << user[i].name << endl;
	}
}
Why not use
void AddUser ( GameScores & user[index], int numS);
instead?

numScores is set to zero. Are you changing that first?

line 123:
user[index].scores
should be
user[index].scores[i]

You made the same mistake in line 148.

You should really have more error checking in this program as well. What happens if I set numScores to 10, or "a string"?
Last edited on
Replace line 123 with cout << user[index].scores[index] << endl;.

Replace line 148 with cout << "score " << i << " = " << user[index].scores[i] << endl;.

Line 48, add a default constructor to GameScore instead of initializing using a for-loop.

Line 29 and 43, the user of the program CAN enter more than 5 scores, causing memory corruption.

Line 34 to 37, how do you know the caller of the of the function won't give an out of range index, you could be corrupting memory that another program is using. I recommend you rewrite the whole thing using std::vector.
Last edited on
Thank you Yay295 and benbalach,

The fix was for lines 123 and 148: user[index].scores[i]

As for error checking, I am the only one using the program and I know its limitations. Rather than spent time on writing code for error checking, I spend my time on learning the concepts of the C++ language like struct and pointers.

Yes, it could be done now that Yay295 pointed out to use void AddUser... instead. However, the book I am reading for all of the examples returned an object. I did the same initially for the Display functions. It was later that I realized based on a earlier chapter that for printing functions most of the time it would be void.

I have not yet learned vectors.

I used the for loop for initializing because of the garbage values I was getting as for the display outputs. I have not yet learned how to initialize variables inside a struct.
Topic archived. No new replies allowed.