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 . . .";
}
|