Saturday, May 27, 2017

Programming Challenge 11.6 - Soccer Scores

/* Soccer Scores - This program stores the following data about a soccer
    player in a structure:
  
        * Player's Name
        * Player's Number
        * Points Scored by Player

    The program keeps an array of 12 of these structures. Each element is
    for a different player on a team. When the program runs, it asks the
    user to enter the data for each player. It then shows a table that lists
    each player's number, name and points scored. The program calculates and
    displays the total points earned by the team. The number and name of the
    player who has earned the most points is also displayed.
  
    Input Validation: No negative values for player's numbers or points scored
    are accepted. */

#include "Utility.h"

struct TeamData
{
    string playerName;        /* Player's name              */
    int    tricotNumber;        /* Player's tricot number */
    int    pointsScored;        /* Player's points          */
};

struct SeasonData
{
    string topPlayer;            /* Top player's name                               */
    int    highscore;            /* The highest score achieved by a player */
    int    totalPoints;        /* Total points scored by the whole team  */
    int    topPlayerNum;        /* Top player's tricot number                    */
};

void initStructure(TeamData [], const int);
void getPlayerData(TeamData [], const int);
bool validateInput(TeamData [], const int);
void getTopPlayer(TeamData [], SeasonData &, const int);
void getTotalScore(TeamData [], SeasonData &, const int);
void displayTeamData(const TeamData [], const SeasonData &, const int);

int main()
{
    const int NUM_PLAYERS = 12;

    /* Array of structures */
    TeamData team[NUM_PLAYERS];

    /* SeasonData Structure */
    SeasonData players = { "\0", 0, 0, 0 };

    initStructure(team, NUM_PLAYERS);
    getPlayerData(team, NUM_PLAYERS);
    getTopPlayer(team, players, NUM_PLAYERS);
    getTotalScore(team, players, NUM_PLAYERS);
    displayTeamData(team, players, NUM_PLAYERS);

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: initStructure

    This function accepts an array of structures as parameter.
    It initializes all members of the structure to default
    values.
   ********************************************************** */

void initStructure(TeamData pData[], const int NUM_PLAYERS)
{
    for (int index = 0; index < NUM_PLAYERS; index++)
    {
        pData[index].playerName   = "\0";
        pData[index].tricotNumber = 0;
        pData[index].pointsScored = 0;
    }
}

/* **********************************************************
   Definition: getPlayerData

    This function accepts an array of structures as parameter.
    The user is asked to enter data for twelve players of a
    soccer team. This data is stored in the appropriate member
    variables of the structure.
   ********************************************************** */

void getPlayerData(TeamData pData[], const int NUM_PLAYERS)
{
    bool valid = false;

    cout << "\n\tF.C. TOKYO - PLAYER STATISTICS\n\n";

    for (int index = 0; index < NUM_PLAYERS; index++)
    {
        cout << "\n\tEnter the name of Player # "
              << (index + 1) << ": ";
        getline(cin, pData[index].playerName);

        cout << "\tEnter " << pData[index].playerName
              << "'s tricot number: ";
        cin >> pData[index].tricotNumber;

        valid = validateInput(pData, index);

        cout << "\tEnter " << pData[index].playerName
              << "'s points scored: ";
        cin >> pData[index].pointsScored;

        valid = validateInput(pData, index);

        cin.ignore();
    }
}

/* **********************************************************
   Definition: validateInput

    This function accepts an array of structures as argument.
    It validates the input.
   ********************************************************** */

bool validateInput(TeamData pData[], const int index)
{
    bool valid = false;

    while (pData[index].tricotNumber < 0)
    {
        cout << "\n\tPlease enter a valid tricot number.\n\n"
              << "\tEnter " << pData[index].playerName
              << "'s tricot number: ";
        cin >> pData[index].tricotNumber;
    }

    while (pData[index].pointsScored < 0)
    {
        cout << "\n\tPlease enter a valid score.\n\n"
             << "\tEnter " << pData[index].playerName
              << "'s points scored: ";
        cin >> pData[index].pointsScored;
    }

    return valid;
}

/* **********************************************************
   Definition: getTopPlayer

    This function accepts an array of structures, as well as a
    structure reference variable as parameters. It determines
    the highest score achieved by a player. The score, name
    and tricot-number of this player is stored in the member
    variables of the SeasonData structure.
   ********************************************************** */

void getTopPlayer(TeamData pData[], SeasonData &players,
                        const int NUM_PLAYERS)
{
    players.highscore = pData[0].pointsScored;

    for (int index = 0; index < NUM_PLAYERS; index++)
    {
        if (pData[index].pointsScored >= players.highscore)
        {
            players.highscore    = pData[index].pointsScored;
            players.topPlayer    = pData[index].playerName;
            players.topPlayerNum = pData[index].tricotNumber;
        }
    }
}

/* **********************************************************
   Definition: getTotalScore

    This function accepts an array of structures, as well as a
    structure reference variable as parameters. It calculates
    and stores the total score achieved during the season.
   ********************************************************** */

void getTotalScore(TeamData pData[], SeasonData &players,
                         const int NUM_PLAYERS)
{
    for (int index = 0; index < NUM_PLAYERS; index++)
    {
        players.totalPoints += pData[index].pointsScored;
    }
}

/* **********************************************************
   Definition: displayData

    This function accepts an array of structures, as well as a
    structure reference variable as parameters. It displays:
  
        * The player's names         
        * The player's numbers
        * The player's scores
        * The player's name who achieved the highest score
        * The player's tricot-number  
        * The player's score
   ********************************************************** */

void displayTeamData(const TeamData pData[], const SeasonData &players,
                            const int NUM_PLAYERS)
{

    cout << "\n\n\n\tF.C. TOKYO - SEASON RESULTS\n\n\n\t";

    cout << setw(17) << left  << "PLAYER NAME"
          << setw(13) << right << "PLAYER NUMBER"
          << setw(16) << right << "PLAYER SCORE" << "\n\t";
  
    cout << setw(47) << setfill('-') << "\n" << setfill(' ');
  
    for (int index = 0; index < NUM_PLAYERS; index++)
    {
        cout << "\t";
        cout << setw(17) << left  << pData[index].playerName
              << setw(13) << right << pData[index].tricotNumber
              << setw(12) << right << pData[index].pointsScored << " pts\n";
    }

    cout << "\t" << setw(47) << setfill('-')
          << "\n"                 << setfill(' ');

    cout << "\tOverall Team Score " << setw(23) << right
          << players.totalPoints << " pts\n";

    cout << "\n\n\tTOP PLAYER\n\t";

    cout << setw(47) << setfill('-') << "\n" << setfill(' ') << "\t";

    cout << setw(17) << left  << players.topPlayer
          << setw(13) << right << players.topPlayerNum
          << setw(12) << right << players.highscore << " pts";    
}

Example Output:






No comments:

Post a Comment