I have been working on a program and it almost totally works. This is what it's supposed to show:
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 below average
Name Score
Bob 3245
Sue 1098
Pat 3217
Press any key to continue . . .
It does all of that but list the players who scored below average at the end. It goes from "Name" "Score", skips the actual names and scores and then shows "Press any key to continue...". Can anyone see what I might be missing here. Here is my code:
#include <iostream>
#include <iomanip>
#include <string>
#include <limits>
using namespace std;
//prototype functions
void inputData(string name[], int score[], int &numPlayers);
void displayData(string name[], int score[], int numPlayers);
double calAvgScore(int score[], int numPlayers);
void belowAvg(string name[], int score[], int numPlayers, double avgScore);
//main
int main()
{
//declaration
const int ARRAY_SIZE = 100;
string name[ARRAY_SIZE];
int score[ARRAY_SIZE];
int numPlayers = 0;
double avgScore = 0;
//welcome
cout << "Welcome to the Video Game Player Program" << endl;
cout << "" << endl;
cout << "" << endl;
//call functions
inputData(name, score, numPlayers);
displayData(name, score, numPlayers);
calAvgScore(score, numPlayers);
belowAvg(name, score, numPlayers, avgScore);
system("pause");
return 0;
}
//function inputData
void inputData(string name[], int score[],
int &numPlayers)
{
while(numPlayers < 100)
{
cout << "Enter Player Name (Q to quit): " << endl;
getline(cin, name[numPlayers]);
//while statement
if(name[numPlayers] == "Q" ||
name[numPlayers] == "q")
break;
cout << "Enter the score for " << name[numPlayers] << endl;
cin >> score[numPlayers];
cin.ignore(numeric_limits<streamsize>::m…
numPlayers += 1;
}
}
//function displayData
void displayData(string name[], int score[], int numPlayers)
{
int i;
cout << setw(10) << left << "Name" << setw(5)
<< right << "Score" << endl;
for(i = 0; i < numPlayers; i++)
{
cout << setw(10) << left << name[i] <<
setw(5) << right << score[i] << endl;
i++;
}
}
//function calAvgScore
double calAvgScore(int score[], int numPlayers)
{
int i;
double avgScore;
double total;
for(i = 0, total = 0; i < numPlayers; i++)
{
total += score[i];
}
avgScore = total / i;
cout << "Average score is: " << avgScore << endl;
return avgScore;
}
//function belowAvg
void belowAvg (string name[], int score[], int numPlayers, double avgScore)
{
int i;
cout << "These players are below the average" << endl;
cout << "" << endl;
cout << setw(10) << left << "Name" << setw(10)
<< right << "Score" << endl;
for(i = 0; i < numPlayers; i++)
{
if(score[i] < avgScore)
cout << setw(10) << left << name[i] << setw(10)
<< right << score[i] << endl;
cout << "" << endl;
cout << "" << endl;
cout << "" << endl;
}
}