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

Run your program in a debugger. Set a breakpoint at line 32. Inspect the value of avgScore. Get surprised. Figure out why it is so.
If you don't know how to use a debugger, print avgScore after line 31.

Run your program in a debugger. Set a breakpoint at line 32. Inspect the value of avgScore. Get surprised. Figure out why it is so.
If you don't know how to use a debugger, print avgScore after line 31.

I see what the problem is, but can't figure out how to fix it. I've literally tried tweaking it for hours. Maybe it's because I've been doing it for so long, but my brain is fried to this stupid program. I have the break set at line 32 and instead of having the player average, the avgScore variable returns as -9.255....e+061. I don't get why. I have return avgScore at the end of the calAvgScore function which should return that number to be used again in the belowAvg function, but it doesn't. It works fine in the calAvgScore function so why does it go to hell between there and in the belowAvg function?

I have a couple hours left before this is due and I've worked on it all week. Most of that work has been trying to figure out this little crap.

Use the return value, initialize avgScore with average score...

avgScore = calAvgScore(...)

Edited 5 Years Ago by vidit_X: n/a

Use the return value, initialize avgScore with average score...

avgScore = calAvgScore(...)

My apologies for being annoying, but could you be more specific? I tried several variations of what you were saying and I couldn't get any positive results.

Use the return value, initialize avgScore with average score...

avgScore = calAvgScore(...)

Nevermind, I got what you were saying. Thank you sooooooo much. I worked on trying to fix this problem longer than I worked on the rest of the program.

This article has been dead for over six months. Start a new discussion instead.