I can not get my code to work I believe it is the part where
I can not get my code to work. I believe it is the part where I press q.
Write a program to do the following: In main, declare a PlayerName Array and a Score Array. Declare the size of the arrays to be 100. In the InputData function, input the player name and score into the arrays for an unknown number of players up to 100. In the DisplayPlayerData function, display the name and score of each player. In the CalculateAverageScore function, calculate the average score and return it by value. In the DisplayBelowAverage function, display the name and score for any player who scored below the average. Do not use global variables.
#include<iostream>
#include <cstring>
#include <string>
using namespace std;
int inputData(string playerNameArray[], int scoreArray[])
{
int count = 0;
while (1) {
cout << \"Enter Player Name(Q to quit): \";
cin >> playerNameArray[count];
if (playerNameArray =\'q\' || playerNameArray = \'Q\')
{
return count;
}
cout << \"Enter score for \"<< playerNameArray[count] << \": \";
cin >> scoreArray[count];
count++;
}
}
void displayPlayerData(string playerNameArray[], int scoreArray[], int count) {
cout << \"Name Score\ \";
for (int i = 0; i<count; i++) {
cout << playerNameArray[i] << \" \" << scoreArray[i] << endl;
}
}
double calculateAverageScore(int scoreArray[], int count) {
int sum = 0;
for (int i = 0; i<count; i++) {
sum += scoreArray[i];
}
return ((double)sum / count);
}
void displayBelowAverage(string playerNameArray[], int scoreArray[], int count, double average) {
cout << \"Players who scored below average\ \";
cout << \"Name Score\ \";
for (int i = 0; i<count; i++) {
if (scoreArray[i] < average) {
cout << playerNameArray[i] << \" \" << scoreArray[i] << endl;
}
}
}
int main() {
string playerNameArray[100];
int scoreArray[100];
int count = inputData(playerNameArray, scoreArray);
displayPlayerData(playerNameArray, scoreArray, count);
double average = calculateAverageScore(scoreArray, count);
cout << \"Average Score: \" << average << endl;
displayBelowAverage(playerNameArray, scoreArray, count, average);
return 0;
system(\"pause\");
}
Solution
Hi,
I have fixed your code. Now it is error free and working fine and highlighted the code changes below.
#include<iostream>
#include <cstring>
#include <string>
using namespace std;
int inputData(string playerNameArray[], int scoreArray[])
{
int count = 0;
while (1) {
cout << \"Enter Player Name(Q to quit): \";
cin >> playerNameArray[count];
if (playerNameArray[count] ==\"q\" || playerNameArray[count] == \"Q\")
{
return count;
}
cout << \"Enter score for \"<< playerNameArray[count] << \": \";
cin >> scoreArray[count];
count++;
}
}
void displayPlayerData(string playerNameArray[], int scoreArray[], int count) {
cout << \"Name Score\ \";
for (int i = 0; i<count; i++) {
cout << playerNameArray[i] << \" \" << scoreArray[i] << endl;
}
}
double calculateAverageScore(int scoreArray[], int count) {
int sum = 0;
for (int i = 0; i<count; i++) {
sum += scoreArray[i];
}
return ((double)sum / count);
}
void displayBelowAverage(string playerNameArray[], int scoreArray[], int count, double average) {
cout << \"Players who scored below average\ \";
cout << \"Name Score\ \";
for (int i = 0; i<count; i++) {
if (scoreArray[i] < average) {
cout << playerNameArray[i] << \" \" << scoreArray[i] << endl;
}
}
}
int main() {
string playerNameArray[100];
int scoreArray[100];
int count = inputData(playerNameArray, scoreArray);
displayPlayerData(playerNameArray, scoreArray, count);
double average = calculateAverageScore(scoreArray, count);
cout << \"Average Score: \" << average << endl;
displayBelowAverage(playerNameArray, scoreArray, count, average);
return 0;
system(\"pause\");
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter Player Name(Q to quit): Suresh
Enter score for Suresh: 50
Enter Player Name(Q to quit): Sekhar
Enter score for Sekhar: 70
Enter Player Name(Q to quit): Anshu
Enter score for Anshu: 80
Enter Player Name(Q to quit): q
Name Score
Suresh 50
Sekhar 70
Anshu 80
Average Score: 66.6667
Players who scored below average
Name Score
Suresh 50


