Solved

Need Help with a C++ program I have written

Posted on 2014-02-07
6
274 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 74

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now