So I am trying to write this code that only accepts 10 inputs into the array. But when I put the 10th input it crashes and I am very stuck. Here are the specs and any help would be appreciated.
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
|
#include <iostream>
#include <string>
using namespace std;
// Input data prototype
void InputData(string PlayerName[], int Score[]);
// Display Player prototype
void DisplayPlayerData(string PlayerName[], int Score[]);
double CalculateAverage(string PlayerName[], int Score[]);
void DisplayAboveAverage(string PlayerName[], int Score[], double avg);
int i = 0;
int main()
{
const int Size = 11;
string PlayerName[Size];
int Score[Size];
InputData(PlayerName, Score);
DisplayPlayerData(PlayerName, Score);
double avg = CalculateAverage(PlayerName, Score);
DisplayAboveAverage(PlayerName, Score, avg);
return 0;
}
void InputData(string PlayerName[], int Score[])
{
while (PlayerName[i] != "q" && PlayerName < [Size])
{
cout << "Enter Player Name (Q to quit) : ";
cin >> PlayerName[i];
if (PlayerName[i] == "q")
{
break;
}
cout << endl;
cout << "Enter score for " << PlayerName[i] << ": ";
cin >> Score[i];
cout << endl;
i = i + 1;
}
cout << endl;
}
void DisplayPlayerData(string PlayerName[], int Score[])
{
int j;
cout << "Name\t" << "Score" << endl;
for (j = 0; j <i; j++)
{
cout << PlayerName[j] << "\t" << Score[j] << endl;
}
return;
}
double CalculateAverage(string PlayerName[], int Score[])
{
int sum = 0;
for (int j = 0; j<i; j++)
{
sum = sum + Score[j];
}
return sum / i;
}
void DisplayAboveAverage(string PlayerName[], int Score[], double avg)
{
cout << endl << "average is ::" << avg;
cout << endl << "players below average are::" << endl;
for (int j = 0; j<i; j++)
{
if (Score[j] > avg)
cout << PlayerName[j] << "::" << Score[j] << endl;
}
}
|