Solved

Need Help with a C++ program I have written

Posted on 2014-02-07
6
283 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MFC program: How do I figure out what object an InvokeHelper is calling? 1 237
help with SQL 15 126
Need some help with delete a recordset 7 141
Help needed to format date/time 3 72
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

829 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