Trouble with array program

This program is supposed to collect player's names and scores in different arrays and then display them to the user. It is also supposed to average the players scores and then display the below average scores. I can only get the first function to work. I'm having no success with the rest of it. Can someone please tell me what I am doing wrong here?
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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

const int ARRAY_SIZE = 100;
void InputData(string playerNameAr[], int scoreAr[], int &numPlayersRef);
void DisplayPlayerData(const string playerNameAr[], const int scoreAr[], int numPlayers);
double CalculateAverageScore(const int scoreAr[], int numPlayers);
void DisplayBelowAverage(const string playerNameAr[], const int scoreAr[], int numPlayers, double averageScore);

void main()
{
	string playerNameAr[ARRAY_SIZE];
	int scoreAr[ARRAY_SIZE];
	int numPlayers = 0;
	int numPlayersRef = 0;
	double averageScore = 0;

	cout << fixed << showpoint << setprecision(2);
	InputData(playerNameAr, scoreAr, numPlayersRef);
	DisplayPlayerData(playerNameAr, scoreAr, numPlayers);
	CalculateAverageScore(scoreAr, numPlayers);
	DisplayBelowAverage(playerNameAr, scoreAr, numPlayers, averageScore);
	system("PAUSE");
}
void InputData(string playerNameAr[], int scoreAr[], int &numPlayersRef)
{
	while(numPlayersRef < ARRAY_SIZE)
	{
		cout << "Please enter player's name (press Q to quit):  ";
		getline(cin, playerNameAr[numPlayersRef], '\n');
		cout << endl;
		if ((playerNameAr[numPlayersRef] == "Q") || (playerNameAr[numPlayersRef] == "q"))
			break;
		cout << "Please enter " << playerNameAr[numPlayersRef] << "'s score:  ";
		cin >> scoreAr[numPlayersRef];
		cout << endl;
		cin.ignore();
		numPlayersRef++;
	}
}

void DisplayPlayerData(const string playerNameAr[], const int scoreAr[], int numPlayers)
{
	cout << setw(10) << left << "\n Name"
		<< setw(5) << right << "Score" << endl;
	for (int i = 0; i < numPlayers; i++)
	{ 
		cout << setw(10) << left << playerNameAr[numPlayers] 
		<< setw(5) << right << scoreAr[numPlayers] << endl;
	}
}

double CalculateAverageScore(const int scoreAr[], int numPlayers)
{
	int i;
	double averageScore = 0, totalScore;
	for (i = 0, totalScore = 0; i < numPlayers; i++)
	{
		totalScore += scoreAr[i];
	}
	averageScore = totalScore/i;
	cout << fixed << showpoint << setprecision(2);
	cout << averageScore << endl << endl;
	return averageScore;
}
void DisplayBelowAverage(const string playerNameAr[], const int scoreAr[], int numPlayers, double averageScore)
{
	cout << "Players who scored below average\n";
	cout << setw (10) << left << "Name"
		<< setw(5) << right << "Score" << endl;
	for (int i = 0; i < numPlayers; i++)
		if(scoreAr[i] < averageScore)
			cout << setw (10) << left << playerNameAr[i]
		<< setw(5) << right << scoreAr[i] << endl;
}
Last edited on
Please edit your post and put the source inside code tags. It will make your post a lot more legible and folks here will be more likely to look at it.
Thank you for your advice. I am new to C++ and new to this website. Please let me know if this is in the proper format. Thanks.
The format's fine now, but more importantly, we need to know what's supposed to be wrong with the code.
"Not working" is not a very useful description.
I took me a while but I figured it out.
Hint: "averageScore"
There are some errors - small, but very significant.

You use the variable numPlayersRef to extract the number of players with InputData. But then you use numPlayers everywhere else.

You never assign averageScore in main.

In DisplayPlayerData, you use numPlayers inside the loop.

Regards
Topic archived. No new replies allowed.