dimanche 2 avril 2017

Showing Below Average Scores in Player Scoring Program

I'm trying to create a program in which the user can enter up to 100 player names and scores, and then have it print out all the players' names and scores, followed by an average of the scores, and finally, display players whose scores were below average. I've managed to do all of that except for the final piece, displaying below average scores. I'm kind of unsure about how to go about it. In my DesplayBelowAverage function, I've attempted to have it read the current player's score and compare it to the average to see if it should be printed out as a below average score, but it doesn't seem to recognize the averageScore value I created in the CalculateAverageScores function. Here's my code:

#include <iostream>
#include <string>

using namespace std;

int InputData(string [], int [], int);
int CalculateAverageScores(int [], int);
void DisplayPlayerData(string [], int [], int);
void DisplayBelowAverage(string [], int [], int);


void main()
{
    string playerNames[100];
    int scores[100];


    int sizeOfArray = sizeof(scores);
    int sizeOfEachElement = sizeof(scores[0]);
    int numberOfElements = sizeOfArray / sizeOfEachElement;

    cout << numberOfElements << endl;

    int numberEntered = InputData(playerNames, scores, numberOfElements);

    DisplayPlayerData(playerNames, scores, numberEntered);

    CalculateAverageScores(scores, numberEntered);


    cin.ignore();
    cin.get();
}

int InputData(string playerNames[], int scores[], int size)
{
    int index;  

    for (index = 0; index < size; index++)
    {
        cout << "Enter Player Name (Q to quit): ";
        getline(cin, playerNames[index]);
        if (playerNames[index] == "Q")
        {
            break;
        }

        cout << "Enter score for " << playerNames[index] << ": ";
        cin >> scores[index];
        cin.ignore();
    }

    return index;
}


void DisplayPlayerData(string playerNames[], int scores[], int size)
{
    int index;

    cout << "Name     Score" << endl;

    for (index = 0; index < size; index++)
    {       
        cout << playerNames[index] << "     " << scores[index] << endl;     
    }
}

int CalculateAverageScores(int scores[], int size)
{
    int index;
    int totalScore = 0;
    int averageScore = 0;

    for (index = 0; index < size; index++)
    {       
        totalScore = (totalScore + scores[index]);              
    }
    averageScore = totalScore / size;
    cout << "Average Score: " << averageScore;

    return index;
}

void DisplayBelowAverage(string playerNames[], int scores[], int size)
{
    int index;

    cout << "Players who scored below average" << endl;
    cout << "Name     Score" << endl;

    for (index = 0; index < size; index++)
    {       
        if(scores[index] < averageScore)
        {
            cout << playerNames[index] << "     " << scores[index] << endl;
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire