Solved

Need Help with a C++ program I have written

Posted on 2014-02-07
6
282 Views
Last Modified: 2014-02-07
Hello everyone. I have written the program below and I get no errors but for whatever reason my Function (Display below average) is not printing any output. Does anyone have any suggestions? Thanks in advance for your help!

//-------------------------------------------------
//Programming Assignment: LAB5A
//Developer:              Hollis R Burks
//Date Written:           2-7-14
//Purpose:                Video Game Player Program
//-------------------------------------------------

#include <iostream>
#include<iomanip>
#include <string>

using namespace std;

// Function's proto-types
void InputData(string playerNameAr[], int scoreAr[], int &numPlayersRef);
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);


// constants
const int array_size=100;
string playerNameAr[array_size];
int numPlayers = 0;
int scoreAr[array_size];
double averageScore;




//main Body code
int main()
{cout << fixed << showpoint << setprecision(2);
	//Call the inputData function
	InputData(playerNameAr,scoreAr, numPlayers);
	
	//Call the DisplayPlayerData function
	DisplayPlayerData(playerNameAr,scoreAr, numPlayers);

	//Call the CalculateAverageScore function and assign the returned value in average score
	CalculateAverageScore(scoreAr, numPlayers);

	//Call the DisplayBelowAverage function
	DisplayBelowAverage(playerNameAr,scoreAr, numPlayers,averageScore);

return 0;

}

//inputDataFunctionCode
void InputData(string playerNameAr[], int scoreAr[], int &numPlayersRef)
{//While the number of players is less than the length of the array
	while (numPlayersRef < array_size)	
	{//Prompt for the player's name
		cout << "Enter Player Name (Q to Quit) : " << endl;
		cin >> playerNameAr[numPlayersRef];

		//If the user entered Q, break out of the loop
		if ((playerNameAr[numPlayersRef] == "Q") || (playerNameAr[numPlayersRef] == "q"))
		{break;}

		//Prompt the user for the player's score
		cout << "Enter Score For : " << endl;
		cin >> scoreAr[numPlayersRef];
		cin.ignore(50, '\n');
		numPlayersRef++;}}




//displayPlayerData function Code
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[i] << setw(5) << right << scoreAr[i] << endl;
}

//calculateAverageScore function Code
double CalculateAverageScore(const int scoreAr[], int numPlayers)
{
    int i;
    double averageScore, totalScore;
    for(i = 0, totalScore = 0; i < numPlayers; i++)
         totalScore += scoreAr[i];
    averageScore = totalScore / numPlayers;
    cout << "\nAverage Score: " << averageScore << endl << endl;
    return averageScore;
}


//displayBelowAverage Function Code

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

Open in new window

0
Comment
Question by:hcaburks
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39842253
Side comment:  I realize you are learning to code, so take this as advice, not criticism. When you comment your code, comment why you are doing something, not what you are doing. Unless you are writing complete garbage (which most of us have done in the beginnings of our careers), it is easy for another programmer to read the code and see what you are doing. What's more difficult to grasp is why a programmer did something the way that he did.
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39842334
what is the output that you are getting currently
0
 

Author Comment

by:hcaburks
ID: 39842375
Here is my current output. It should have Bob Sue and Pat and there scores in the Below average section. Instead it returns nothing.
lab5AOutput.docx
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 26

Expert Comment

by:arober11
ID: 39842463
Hi

To start with you only have one true constant array_size, the rest are variables, so a slightly misleading comment. The two arrays could be left where they are but I'd personally stick them along with a definite variable averageScore in the main function.  As to the reason why your getting nothing out, it's down to you not doing anything with the results from your CalculateAverageScore() call, nothing a averageScore =  can't solve.

Try:

// constants
const int array_size=100;

//main Body code
int main() {

        string playerNameAr[array_size];
        int numPlayers = 0;
        int scoreAr[array_size];
        double averageScore;

        cout << fixed << showpoint << setprecision(2);
        //Call the inputData function
        InputData(playerNameAr,scoreAr, numPlayers);

        //Call the DisplayPlayerData function
        DisplayPlayerData(playerNameAr,scoreAr, numPlayers);

        //Call the CalculateAverageScore function and assign the returned value in average score
        averageScore = CalculateAverageScore(scoreAr, numPlayers);

        //Call the DisplayBelowAverage function
        DisplayBelowAverage(playerNameAr,scoreAr, numPlayers,averageScore);

return 0;

}

Open in new window

0
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39842465
the average score is not passed back to the main function, although it is caliculated in the caliculate averge score function....

//Call the CalculateAverageScore function and assign the returned value in average score
CalculateAverageScore(scoreAr, numPlayers);


should be written like below

//Call the CalculateAverageScore function and assign the returned value in average score
averageScore = CalculateAverageScore(scoreAr, numPlayers);
0
 

Author Closing Comment

by:hcaburks
ID: 39842510
Thank you for that. I figured it was something easy I was just missing.
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question