Player Scores Arrays with functions project

I am new to C++ and am having a hard time with my current project. I have this set of code but I have 2 errors.

1. I have a GLOBAL VARIABLE and the program cannot use a GLOBAL VARIABLE.
2. After the 10th player is entered the program crashes.

Can anyone please lend me a hand with this problem? It would be greatly appreciated. Here are the parameters of the assignment:

Write a program that does the following:
• It places player names and their scores into two appropriate arrays.
• It displays player names and their scores in a formatted output
• It calculates the average score and outputs it on the screen
• It displays names and scores of players whose score is above average
Use functions to implement this program:
• The main( ) function declares a PlayerName array and a Score array. The size of the arrays is 10.
• The InputData( ) function, inputs the player name and score into the arrays for an unknown number of players up to 10.
• The DisplayPlayerData( ) function, displays the name and score of each player.
• The CalculateAverageScore( ) function, calculates the average score and returns it by value.
• The DisplayAbovAverage( ) function, displays the name and score for any player who scored above the average. Do not use global variables.

An example for program output is as follows:

Enter Player Name (Q to quit): Bob
Enter score for Bob: 3245
Enter Player Name (Q to quit): Sue
Enter score for Sue: 1098
Enter Player Name (Q to quit): Dave
Enter score for Dave: 8219
Enter Player Name (Q to quit): Pat
Enter score for Pat: 3217
Enter Player Name (Q to quit): Q

Name Score
Bob 3245
Sue 1098
Dave 8219
Pat 3217

Average Score: 3944.75

Players who scored above average
Name Score
Dave 8219
Press any key to continue . . .

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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

void InputData(string PlayerName[], int Score[]);
void DisplayPlayerData(string PlayerName[], int Score[]);
double CalculateAverageScore(string PlayerName[], int Score[]);
void DisplayAboveAverage(string PlayerName[], int Score[], double average);

int size = 0;

int main()
{
	
	string PlayerName[10];
	int Score[10];

	InputData(PlayerName, Score);

	DisplayPlayerData(PlayerName, Score);
		
	double average = CalculateAverageScore(PlayerName, Score);

	DisplayAboveAverage(PlayerName, Score, average);

	cin.ignore();
	cin.get();
	return 0;
}

void InputData(string PlayerName[], int Score[])
{


	while (PlayerName[size] != "q" || PlayerName[size] != "Q")
	{
		cout << "Enter Player Name (Q to quit): ";
		cin >> PlayerName[size];

		if (PlayerName[size] == "q" || PlayerName[size] == "Q")
		{
			break;
		}

		cout << "Enter score for " << PlayerName[size] << ": ";
		cin >> Score[size];
		size = size + 1;
	}
	cout << endl;
}

void DisplayPlayerData(string PlayerName[], int Score[])
{

	cout << "  NAME" << "\t" << "SCORE" << endl;

	for (int y = 0; y < size; y++)
	{
		cout << PlayerName[y] << "\t    " << Score[y] << endl;
	}
	cout << endl;
}

double CalculateAverageScore(string PlayerName[], int Score[])
{
	int sum = 0;

	for (int w = 0; w < size; w++)
	{
		sum = (sum + Score[w]);
	} 
	return (sum / size);
}

void DisplayAboveAverage(string PlayerName[], int Score[], double average)
{
	cout << "Average Score: " << average << endl << endl;
	cout << "Players who scored above average" << endl;
	cout << "  NAME" << "\t" << "SCORE" << endl;

	for (int z = 0; z < size; z++)
	{
		if (Score[z] > average)
		{
			cout << PlayerName[z] << "\t    " << Score[z] << endl;
		}
	}
	cout << "Press any key to continue . . .";
	
}
Last edited on
Have you tried moving the variable size inside the Main() function and then passing that var to the functions that require its value?
yes I tried but maybe I was going about it wrong. how would I go about that?
Okay so I figured out the GLOBAL VARIABLE issue because I was not sending the InputData function variable size by reference.

Now I am having an issue still with the following

After the 10th player is entered the program crashes.

now I know it has something to do with the function for InputData and the parameters of the loop but I am still running circles in my head right now.

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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

void InputData(string PlayerName[], int Score[], int &size);
void DisplayPlayerData(string PlayerName[], int Score[], int &size);
double CalculateAverageScore(string PlayerName[], int Score[], int &size);
void DisplayAboveAverage(string PlayerName[], int Score[], double average, int &size);



int main()
{
	
	string PlayerName[10];
	int Score[10];
	int size = 0;

	InputData(PlayerName, Score, size);

	DisplayPlayerData(PlayerName, Score, size);
		
	double average = CalculateAverageScore(PlayerName, Score, size);

	DisplayAboveAverage(PlayerName, Score, average, size);

	cin.ignore();
	cin.get();
	return 0;
}

void InputData(string PlayerName[], int Score[], int &size)
{


	while (PlayerName[size] != "q" || PlayerName[size] != "Q")
	{
		cout << "Enter Player Name (Q to quit): ";
		cin >> PlayerName[size];

		if (PlayerName[size] == "q" || PlayerName[size] == "Q")
		{
			break;
		}

		cout << "Enter score for " << PlayerName[size] << ": ";
		cin >> Score[size];
		size = size + 1;
	}
	cout << endl;
}

void DisplayPlayerData(string PlayerName[], int Score[], int &size)
{

	cout << "  NAME" << "\t" << "SCORE" << endl;

	for (int y = 0; y < size; y++)
	{
		cout << PlayerName[y] << "\t    " << Score[y] << endl;
	}
	cout << endl;
}

double CalculateAverageScore(string PlayerName[], int Score[], int &size)
{
	double sum = 0;

	for (int w = 0; w < size; w++)
	{
		sum = (sum + Score[w]);
	} 
	return (sum / size);
}

void DisplayAboveAverage(string PlayerName[], int Score[], double average, int &size)
{
	cout << "Average Score: " << average << endl << endl;
	cout << "Players who scored above average" << endl;
	cout << "  NAME" << "\t" << "SCORE" << endl;

	for (int z = 0; z < size; z++)
	{
		if (Score[z] > average)
		{
			cout << PlayerName[z] << "\t    " << Score[z] << endl;
		}
	}
	cout << "Press any key to continue . . .";
	
}
Last edited on
So looks like I figured it out just in case anyone else needs to do this project here is a fully functional set of code.

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

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

//Program Prototypes
void InputData(string PlayerName[], int Score[], int &size, const int &a);
void DisplayPlayerData(string PlayerName[], int Score[], int &size);
double CalculateAverageScore(string PlayerName[], int Score[], int &size);
void DisplayAboveAverage(string PlayerName[], int Score[], double average, int &size);



int main()
{
	
	//Array functions with the size of 10, the size varriable sends the array size from function to function
	const int a = 10;
	string PlayerName[a];
	int Score[a];
	int size = 0;

	//Calling InputData function this is gathering all the information about the players
	InputData(PlayerName, Score, size, a);

	//Calling DisplayPlayerData function this displays all the players and their respective scores
	DisplayPlayerData(PlayerName, Score, size);
	
	//declaring average a double and making it equal to the function CalculateAverageScore to store and display the functions average of all the scores entered
	double average = CalculateAverageScore(PlayerName, Score, size);

	//Calling the DisplayAboveAverage function which displays all the players and their score that is above the average score
	DisplayAboveAverage(PlayerName, Score, average, size);

	cin.ignore();
	cin.get();
	return 0;
}

//InputData function 
void InputData(string PlayerName[], int Score[], int &size, const int &a)
{
	//A while loop to check for the input to stop the program, and once the max number of players is met then the loop breaks
	while (size < a)
	{
		cout << "Enter Player Name (Q to quit): ";
		cin >> PlayerName[size];

		//if statements for the input q or Q to exit the program
		if (PlayerName[size] == "q" || PlayerName[size] == "Q")
		{
			break;
		}

		//if statement for when the max amout of the array is met to quit the program
		if (size == a)
		{
			break;
		}

		//if both the IF statements are not met then the program asks for the score of the player
		cout << "Enter score for " << PlayerName[size] << ": ";
		cin >> Score[size];
		size = size + 1;
	}
	cout << endl;
}

//DisplayPlayerData function
void DisplayPlayerData(string PlayerName[], int Score[], int &size)
{

	cout << "  NAME" << "\t" << "SCORE" << endl;

	//for loop to execute and display all the players and their scores for the variable size
	for (int y = 0; y < size; y++)
	{
		cout << PlayerName[y] << "\t    " << Score[y] << endl;
	}
	cout << endl;
}

//CalculateAverageScore function
double CalculateAverageScore(string PlayerName[], int Score[], int &size)
{
	double sum = 0;

	//for loop to execute the equation for all values that are saved in Score then divided by the size variable
	for (int w = 0; w < size; w++)
	{
		sum = (sum + Score[w]);
	} 
	return (sum / size);
}

//DisplayAboveAverage function
void DisplayAboveAverage(string PlayerName[], int Score[], double average, int &size)
{
	//displays the average
	cout << "Average Score: " << average << endl << endl;
	cout << "Players who scored above average" << endl;
	cout << "  NAME" << "\t" << "SCORE" << endl;

	//for loop to see what values in Score are above the average of all the scores the user input
	for (int z = 0; z < size; z++)
	{
		if (Score[z] > average)
		{
			cout << PlayerName[z] << "\t    " << Score[z] << endl;
		}
	}
	cout << "Press any key to continue . . .";
	
}
Last edited on
Topic archived. No new replies allowed.