Is there any reason for this?
It seems there for no good reason other than to confuse yourself, just initialise the totalScore right where you do the averageScore, silly constructs like that are totally out of context IMO !Code:double averageScore = 0, totalScore; for (i = 0, totalScore = 0; i < numPlayers; i++)
Thought for the day:FLTK: "The most fun you can have with your clothes on.""Are you sure your sanity chip is fully screwed in sir?" (Kryten)
Stroustrup:
"If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"
vanilla cin.ignore() is not sufficient here ... AFAIK .You need a cin.ignore() after each input statement based on how you are doing things.
You need
To clear out all the trailing characters.Code:cin.ignore(numeric_limits<streamsize>::max(),'\n');
btw...you'd need to include <limits> for that.
mana is there any tutorials on how that statement you posted works because I haven't seen anything on limits<streamsize>::max() before. Is this going to have to go after all of my input statements?
I will try to research the <limits> and see if I can research it better then find out if I can use it in this assignment.
Not always, but when the situation is somewhat like; you expect a single character y/n answer and get words like Yes, No, Nope ..and the program needs to use the input for other reasons after that.
numeric_limits can be used to determine the max or min size/range of type passed (and many other safety tricks !) in the <> brackets.
numeric_limits - C++ Reference
Then, the you can easily find out how it works, by examining the prototype of istream::ignore - C++ Reference .
Last edited by manasij7479; 09-29-2011 at 11:36 AM.
ok so my average is working now but the names are still not displaying from the arrays for me and the belowaverage function is not bringing anything up. Any ideas on what I am doing wrong?
Code:#include <iostream> #include <iomanip> #include <string> using namespace std; const int ARRAY_SIZE = 100; void InputData(string playerNameAr[], int scoreAr[], int &numPlayers); void DisplayPlayerData(const string playerNameAr[], const int scoreAr[], int numPlayers); double CalculateAverageScore(const int scoreAr[], int numPlayers); void DisplayBelowAverage(const string playerNameAr[], const int scoreAr[], int numPlayers, double averageScore); int main() { string playerNameAr[ARRAY_SIZE]; int scoreAr[ARRAY_SIZE]; int numPlayers = 0; int numPlayersRef = 0; double averageScore = 0; cout << fixed << showpoint << setprecision(2); InputData(playerNameAr, scoreAr, numPlayers); DisplayPlayerData(playerNameAr, scoreAr, numPlayers); CalculateAverageScore(scoreAr, numPlayers); DisplayBelowAverage(playerNameAr, scoreAr, numPlayers, averageScore); system("PAUSE"); } void InputData(string playerNameAr[], int scoreAr[], int &numPlayers) { while(numPlayers < 100 & playerNameAr[numPlayers]!= "Q") { cout << "Please enter player's name (press Q to quit): "; getline(cin, playerNameAr[numPlayers], '\n'); cout << endl; if ((playerNameAr[numPlayers] == "Q") || (playerNameAr[numPlayers] == "q")) break; cout << "Please enter " << playerNameAr[numPlayers] << "'s score: "; cin >> scoreAr[numPlayers]; cout << endl; cin.ignore(numeric_limits<streamsize>::max(),'\n'); numPlayers++; } } void DisplayPlayerData(const string playerNameAr[], const int scoreAr[], int numPlayers) { cout << setw(10) << left << "\n Name" << setw(5) << right << "Score" << endl; for (int i = 0; i < numPlayers; i++) { cout << setw(10) << left << playerNameAr[numPlayers] << setw(5) << right << scoreAr[numPlayers] << endl; } } double CalculateAverageScore(const int scoreAr[], int numPlayers) { int i; double averageScore = 0, totalScore; for (i = 0, totalScore = 0; i < numPlayers; i++) { totalScore += scoreAr[i]; } averageScore = totalScore/i; cout << fixed << showpoint << setprecision(2); cout << averageScore << endl << endl; return averageScore; } void DisplayBelowAverage(const string playerNameAr[], const int scoreAr[], int numPlayers, double averageScore) { cout << "Players who scored below average\n"; cout << setw (10) << left << "Name" << setw(5) << right << "Score" << endl; for (int i = 0; i < numPlayers; i++) if(scoreAr[i] < averageScore) cout << setw (10) << left << playerNameAr[i] << setw(5) << right << scoreAr[i] << endl; }
Well, let's see:
That is because you never store the result of your CalculateAverageScore function. In main you would need to have:Code://Logical AND is && not & //Also use your constant ARRAY_SIZE here vice your magic number 100 while(numPlayers < 100 & playerNameAr[numPlayers]!= "Q")
Code:averageScore = CalculateAverageScore(scoreAr, numPlayers);
still no luck even with those changes from hunter. Issue still is with displaying the names from the arrays and there scores correctly. The average score function works perfectly but It will not print out the names or scores and the below average function is also not putting any names and scores in at the bottom.
Here is one problem:
Presumably you meant numPlayers to be i.Code:void DisplayPlayerData(const string playerNameAr[], const int scoreAr[], int numPlayers) ... for (int i = 0; i < numPlayers; i++) { cout << setw(10) << left << playerNameAr[numPlayers] << setw(5) << right << scoreAr[numPlayers] << endl; } ...
Applying my fix with previously mentioned fixes makes the output correct for me. If it does not for you, then provide up-to-date code and steps to get incorrect output.
this is my code now which still is not displaying the players names when I enter them and the belowAverage function is still not displaying either.
Code:#include <iostream> #include <iomanip> #include <string> using namespace std; const int ARRAY_SIZE = 100; void InputData(string playerNameAr[], int scoreAr[], int &numPlayers); void DisplayPlayerData(const string playerNameAr[], const int scoreAr[], int numPlayers); double CalculateAverageScore(const int scoreAr[], int numPlayers); void DisplayBelowAverage(const string playerNameAr[], const int scoreAr[], int numPlayers, double averageScore); int main() { string playerNameAr[ARRAY_SIZE]; int scoreAr[ARRAY_SIZE]; int numPlayers = 0; int numPlayersRef = 0; double averageScore = 0; averageScore = CalculateAverageScore(scoreAr, numPlayers); cout << fixed << showpoint << setprecision(2); InputData(playerNameAr, scoreAr, numPlayers); DisplayPlayerData(playerNameAr, scoreAr, numPlayers); CalculateAverageScore(scoreAr, numPlayers); DisplayBelowAverage(playerNameAr, scoreAr, numPlayers, averageScore); system("PAUSE"); } void InputData(string playerNameAr[], int scoreAr[], int &numPlayers) { while(numPlayers < ARRAY_SIZE && playerNameAr[numPlayers]!= "Q") { cout << "Please enter player's name (press Q to quit): "; getline(cin, playerNameAr[numPlayers], '\n'); cout << endl; if ((playerNameAr[numPlayers] == "Q") || (playerNameAr[numPlayers] == "q")) break; cout << "Please enter " << playerNameAr[numPlayers] << "'s score: "; cin >> scoreAr[numPlayers]; cout << endl; cin.ignore(numeric_limits<streamsize>::max(),'\n'); numPlayers++; } } void DisplayPlayerData(const string playerNameAr[], const int scoreAr[], int numPlayers) { cout << setw(10) << left << "\n Name" << setw(5) << right << "Score" << endl; for (int i = 0; i < numPlayers; i++) { cout << setw(10) << left << playerNameAr[numPlayers] << setw(5) << right << scoreAr[i] << endl; } } double CalculateAverageScore(const int scoreAr[], int numPlayers) { int i; double averageScore = 0, totalScore; for (i = 0, totalScore = 0; i < numPlayers; i++) { totalScore += scoreAr[i]; } averageScore = totalScore/i; cout << fixed << showpoint << setprecision(2); cout << averageScore << endl << endl; return averageScore; } void DisplayBelowAverage(const string playerNameAr[], const int scoreAr[], int numPlayers, double averageScore) { cout << "Players who scored below average\n"; cout << setw (10) << left << "Name" << setw(5) << right << "Score" << endl; for (int i = 0; i < numPlayers; i++) if(scoreAr[i] < averageScore) cout << setw (10) << left << playerNameAr[i] << setw(5) << right << scoreAr[i] << endl; }
scratch that I got the names to post properly but the below average function is not working.