Solved

Need Help with a C++ program I have written

Posted on 2014-02-07
6
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 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