Question

In: Computer Science

Write a program that declares a struct to store the data of a football player (player’s...

Write a program that declares a struct to store the data of a football player (player’s name, player’s position, number of touchdowns, number of catches, number of passing yards, number of receiving yards, and the number of rushing yards).

Declare an array of 10 components to store the data of 10 football players.

Your program must contain a function to input data and a function to output data. Add functions to search the array to find the index of a specific player, and up-date the data of a player. (You may assume that the input data is stored in a file.) Before the program terminates, give the user the option to save data in a file. Your program should be menu driven, giving the user various choices.

An example of the program is shown below:

Select one of the following options:                                 
1: To print a player's data                                          
2: To print the entire data                                          
3: To update a player's touch downs                                  
4: To update a player's number of catches                            
5: To update a player's passing yards                                
6: To update a player's receiving yards                              
7: To update a player's rushing yards                                
99: To quit the program                                              
1                                                                    

Enter player's name: Bill                                            

Name: BillPosition: Quarter_Back;  Touch Downs: 70;  Number of Catche
s: 0;  Passing Yards: 8754;  Receiving Yards: 0;  Rushing Yards: 573  

Select one of the following options:                                 
1: To print a player's data                                          
2: To print the entire data                                          
3: To update a player's touch downs                                  
4: To update a player's number of catches                            
5: To update a player's passing yards                                
6: To update a player's receiving yards                              
7: To update a player's rushing yards                                
99: To quit the program                                              
99                                                                   

Would you like to save data: (y,Y/n,N) N

Solutions

Expert Solution

SOLUTION-
I have solve the problem in C++ code with comments and screenshot for easy understanding :)

CODE-

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

struct footballPlayerType{
    string name;
    string position;
    int touchdowns;
    int catches;
    int passingYards;
    int receivingYards;
    int rushingYards;
};

void showMenu();
void getData(ifstream& inf, footballPlayerType list[], int length);
void printPlayerData(footballPlayerType list[], int length, int playerNum);
void printData(footballPlayerType list[], int length);
void saveData(ofstream& outF, footballPlayerType list[], int length);
int searchData(footballPlayerType list[], int length, string n);
void updateTouchdowns(footballPlayerType list[], int length, int tDowns, int playerNum);
void updateCatches(footballPlayerType list[], int length, int catches, int playerNum);

void updatePassingYards(footballPlayerType list[], int length, int passingYards, int playerNum);
void updateReceivingYards(footballPlayerType list[], int length, int receivingYards, int playerNum);
void updateRushingYards(footballPlayerType list[], int length, int rushingYards, int playerNum);

int main(){
    footballPlayerType bigGiants[10];
    int numberOfPlayers = 10;

    int choice;
    string name;
    int playerNum;
    int numOfTouchdowns;
    int numOfCatches;
    int numOfPassingYards;
    int numOfReceivingYards;
    int numOfRushingYards;

    ifstream inFile;
    ofstream outfile;

    inFile.open("Data.txt");

    if(!inFile){
        cout << "Input File Does Not Exist. Program Terminates!" << endl;
        return 1;
    }

    getData(inFile, bigGiants, numberOfPlayers);
    do{
        showMenu();
        cin >> choice;
        cout << endl;

        switch(choice){
            case 1:
                cout << "Enter Player's Name: ";
                cin >> name;
                cout << endl;

                playerNum = searchData(bigGiants, numberOfPlayers, name);
                printPlayerData(bigGiants, numberOfPlayers, playerNum);
                break;

            case 2:
                printData(bigGiants, numberOfPlayers);
                break;

            case 3:
                cout << "Enter Player Name: ";
                cin >> name;
                cout << endl;

                playerNum = searchData(bigGiants, numberOfPlayers, name);

                cout << "Enter Number of Touchdowns to be added: ";
                cin >> numOfTouchdowns;
                cout << endl;

                updateTouchdowns(bigGiants, numberOfPlayers, numOfTouchdowns, playerNum);
                break;

            case 4:
                cout << "Enter Player Name: ";
                cin >> name;
                cout << endl;

                playerNum = searchData(bigGiants, numberOfPlayers, name);

                cout << "Enter Number of Catches to be Added: ";
                cin >> numOfCatches;
                cout << endl;

                updateCatches(bigGiants, numberOfPlayers, numOfCatches, playerNum);
                break;

            case 5:
                cout << "Enter Player Name: ";
                cin >> name;
                cout << endl;

                playerNum = searchData(bigGiants, numberOfPlayers, name);

                cout << "Enter Number of Passing Yards to be Added: ";
                cin >> numOfPassingYards;
                cout << endl;

                updatePassingYards(bigGiants, numberOfPlayers, numOfPassingYards, playerNum);
                break;

            case 6:
                cout << "Enter Player Name: ";
                cin >> name;
                cout << endl;

                playerNum = searchData(bigGiants, numberOfPlayers, name);

                cout << "Enter Number of Receiving Yards to be Added: ";
                cin >> numOfReceivingYards;
                cout << endl;

                updateReceivingYards(bigGiants, numberOfPlayers, numOfReceivingYards, playerNum);
                break;

            case 7:
                cout << "Enter Player Name: ";
                cin >> name;
                cout << endl;

                playerNum = searchData(bigGiants, numberOfPlayers, name);

                cout << "Enter Number of Rushing Yards to be Added: ";
                cin >> numOfRushingYards;
                cout << endl;

                updateRushingYards(bigGiants, numberOfPlayers, numOfRushingYards, playerNum);
                break;

            case 99:
                break;

            default:
                cout << "Invalid Selection!" << endl;
        }
    }
    while (choice != 99);

    char response;

    cout << "Would You Like to Save Data(Y/N)?: ";
    cin >> response;
    cout << endl;

    if (response == 'y' || response == 'Y')
        saveData(outfile, bigGiants, numberOfPlayers);

    return 0;
}

void showMenu(){
    cout << "Select One of the Following Options: " << endl;
    cout << "1. To Print a Player's Data" << endl;
    cout << "2. To Print the Entire Data" << endl;
    cout << "3. To Update a Player's Touchdowns" << endl;
    cout << "4. To Update a Player's Number of Catches" << endl;
    cout << "5. To Update a Player's Passing Yards" << endl;
    cout << "6. To Update a Player's Receving Yards" << endl;
    cout << "7. To Update a Player's Rushing Yards" << endl;
    cout << "99. Quit Program" << endl;
}

void getData(ifstream& inf, footballPlayerType list[], int length){
    for (int i = 0; i < length; i++)
        inf >> list[i].name >> list[i].position >> list[i].touchdowns >> list[i].catches>> list[i].passingYards >> list[i].receivingYards >> list[i].rushingYards;
}

void printPlayerData(footballPlayerType list[], int length, int playerNum){
    if (0 <= playerNum && playerNum < length)
        cout << "Name: " << list[playerNum].name << "; Position: " << list[playerNum].position                                        << "; Touchdowns: " << list[playerNum].touchdowns                                  << "; Number of Catches: " << list[playerNum].catches                              << "; Passing Yards: " << list[playerNum].passingYards                             << "; Receiving Yards: " << list[playerNum].receivingYards                         << "; Rushing Yards: " << list[playerNum].rushingYards << endl << endl;
    else
        cout << "Invalid Player Number!" << endl << endl;
}

void printData(footballPlayerType list[], int length){
    cout << left << setw(10) << "Name" << setw(14) << "Position"                                                            << setw(12) << "Touchdowns"                                                          << setw(9) << "Catches"                                                              << setw(12) << "Passing Yards"                                                       << setw(10) << "Receiving Yards"                                                     << setw(12) << "Rushing Yards" << endl;

    for (int i = 0; i < length; i++)
        cout << left << setw(10) << list[i].name << setw(14) << list[i].position                                           << right << setw(6) << list[i].touchdowns                                                   << setw(9) << list[i].catches                                                      << setw(12) << list[i].passingYards                                                << setw(10) << list[i].receivingYards                                              << setw(12) << list[i].rushingYards << endl;
    cout << endl << endl;
}

void saveData(ofstream& outF, footballPlayerType list[], int length){
    outF.open("Ch9_Ex7Output.txt");

    for (int i = 0; i < length; i++)
        outF << list[i].name << " " << list[i].position<< " " << list[i].touchdowns                                                       << " " << list[i].catches                                                          << " " << list[i].passingYards                                                     << " " << list[i].receivingYards                                                   << " " << list[i].rushingYards << endl;
}

int searchData(footballPlayerType list[], int length, string n){
    for (int i = 0; i < length; i++)
        if (list[i].name == n)
            return i;

    return -1;
}

void updateTouchdowns(footballPlayerType list[], int length, int tDowns, int playerNum){
    if (0 <= playerNum && playerNum < length)
        list[playerNum].touchdowns += tDowns;
    else
        cout << "Invalid Player Number" << endl;
}

void updateCatches(footballPlayerType list[], int length, int catches, int playerNum){
    if (0 <= playerNum && playerNum < length)
        list[playerNum].catches += catches;
    else
        cout << "Invalid Player Number" << endl;
}

void updatePassingYards(footballPlayerType list[], int length, int passYards, int playerNum){
    if (0 <= playerNum && playerNum < length)
        list[playerNum].passingYards += passYards;
    else
        cout << "Invalid Player Number" << endl;
}
void updateReceivingYards(footballPlayerType list[], int length, int recYards, int playerNum){
    if (0 <= playerNum && playerNum < length)
        list[playerNum].receivingYards += recYards;
    else
        cout << "Invalid Player Number" << endl;
}
void updateRushingYards(footballPlayerType list[], int length, int rushYards, int playerNum){
    if (0 <= playerNum && playerNum < length)
        list[playerNum].rushingYards += rushYards;
    else
        cout << "Invalid Player Number" << endl;
}

sample (Data.txt) -

Bill Quarter_Back 70 0 8754 0 573
Jackson Receiver 55 87 50 5490 574
Grahm Running_Back 45 30 0 50 2800
McCoy Full_Back 25 10 0 25 3762
Daryl Quarter_Back 50 2 7560 0 450
Santiago Left_Tackle 5 0 0 0 0
Hanks Receiver 35 37 0 3590 876
Johnson Running_Back 25 80 0 100 4000
Miller Receiver 110 250 150 7867 2100
Ruth Quarter_Back 85 0 12901 0 3249


SCREENSHOT-


IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

Write Program in C: Write a program that: program starts; declares and initializes to 7.25% constant...
Write Program in C: Write a program that: program starts; declares and initializes to 7.25% constant float variable NJSALES_TAX; declares and initializes to 1000 an integer variable total; declares and initializes to zero a float variable grand_total; prompt and input on new line total; calculate grand_total to equal total plus (total*NJSALES_TAX); if grand_total <= 1000 print on new line “Grand total is less than or equal to 1000 it is $” and the grand_total to two decimal places; else if...
Code in python: Write a class that implements a struct. In your struct store Student information...
Code in python: Write a class that implements a struct. In your struct store Student information such as name, netid, and gpa. Write a function that can access a student in a list of 5 structs by netid and print their name and gpa. Show that your function works by calling it.
Write a program (C++) which allows a League of Legends player to both store and output...
Write a program (C++) which allows a League of Legends player to both store and output statistics on their games played. When the program starts it allows the user to enter new entries. The entries contain the following information: Champion Name Kills Assists Deaths Win? Each entry is appended to a file called games.txt. When the user is done entering data the statistics from the games in the file are to be displayed. The average kills, assists, and deaths are...
Create a base class that describes a baseball player. A baseball player’s attributes are: player name...
Create a base class that describes a baseball player. A baseball player’s attributes are: player name player number A pitcher has all the attributes of a baseball player but also has two other attributes: number of innings pitched number of earned runs. All pitchers have an earned run average which is calculated as : (Earned runs / innings pitched ) x 9 A batter has all of the attributes of a baseball player plus two other attributes: number of at...
write a Program in C++ Using a structure (struct) for a timeType, create a program to...
write a Program in C++ Using a structure (struct) for a timeType, create a program to read in 2 times into structures, and call the method addTime, in the format: t3 = addTime(t1, t2); Make sure to use add the code to reset and carry, when adding 2 times. Also, display the resultant time using a function: display(t3);
Write a program that does the following in C++ 1 ) Write the following store data...
Write a program that does the following in C++ 1 ) Write the following store data to a file (should be in main) DC Tourism Expenses 100.20 Revenue 200.50 Maryland Tourism Expenses 150.33 Revenue 210.33 Virginia Tourism Expenses 140.00 Revenue 230.00 2 ) Print the following heading: (should be in heading function) Store name | Profit [Note: use setw to make sure all your columns line up properly] 3 ) Read the store data for one store (should be in...
2. Write a program in C++ that: a) Declares a 1D array A with 30 elements...
2. Write a program in C++ that: a) Declares a 1D array A with 30 elements b) Inputs an integer n from 1-30 from the keyboard. If n < 1 set n = 1. If n > 30 set n = 30. the program should keep asking the user the input n one by one, followed by printing of the value of n (n=n if bigger than 1 and smaller than 30, 1 if smaller than 1 and 30 if...
Write a program that uses a structure to store the following weather data for a particular...
Write a program that uses a structure to store the following weather data for a particular month: Total Rainfall High Temperature Low Temperature Average Temperature. The program should have an array of 12 structures to hold weather data for an entire year. When the program runs, it should ask the user to enter data for each month. (The average temperature should be calculated.) Once the data are entered for all the months, the program should calculate and display the average...
Write a c program that creates a struct to be able to read a .img file...
Write a c program that creates a struct to be able to read a .img file and a .pat file.
Write a C program with a struct that contains a string of 12 characters and two...
Write a C program with a struct that contains a string of 12 characters and two integers: number1 and number2. Read all 3 values in from the keyboard using scanf. Print the string and the sum of the two integers using the fully qualified struct names.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT