C++ Video Game Player Program
ok so I have a program that is supposed to use arrays and this is the week we start learning arrays. The program is supposed to allow the user to input scores for a number of players up to 100 players and then it will post all scores as well as calculate averages and show who has a below average score. My issue is that for some reason I am not getting the multiple players to display nor will any of there scores and the calculateAverage function doesnt seem to be working. Here is the code I have:
Code:
#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;
}