In: Computer Science
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
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!!!!!!!!----------