Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program that declares a struct to store the data of a football player ( player s name, player s position, number of touchdowns,

Write a program that declares a struct to store the data of a football player (players name, players 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.
I currently have the code below written up but it keeps saying its wrong so i dont know what to change.
using namespace std;
struct Player
{
string name;
string position;
int touchDowns;
int catches;
int passingYards;
int receivingYards;
int rushingYards;
};
void inputData(Player[], int);
void outputData(Player[], int);
int search(Player[], int, string);
void update(Player[], int, int);
void saveData(Player[], int);
int main()
{
const int SIZE =10;
Player players[SIZE];
int choice;
int index;
string name;
inputData(players, SIZE);
do
{
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 touch downs" << 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 receiving yards" << endl;
cout <<"7: To update a player's rushing yards" << endl;
cout <<"99: To quit the program" << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter player's name: ";
cin >> name;
index = search(players, SIZE, name);
if (index ==-1)
cout << "Player not found" << endl;
else
{
cout << "Name: "<< players[index].name << endl;
cout << "Position: "<< players[index].position << endl;
cout << "Touch Downs: "<< players[index].touchDowns << endl;
cout << "Number of Catches: "<< players[index].catches << endl;
cout << "Passing Yards: "<< players[index].passingYards << endl;
cout << "Receiving Yards: "<< players[index].receivingYards << endl;
cout << "Rushing Yards: "<< players[index].rushingYards << endl;
}
break;
case 2:
outputData(players, SIZE);
break;
case 3:
case 4:
case 5:
case 6:
case 7:
cout << "Enter player's name: ";
cin >> name;
index = search(players, SIZE, name);
if (index ==-1)
cout << "Player not found" << endl;
else
update(players, index, choice);
break;
case 99:
{
char saveChoice;
cout << "Would you like to save data? (y/Y/n/N): ";
cin >> saveChoice;
if (saveChoice =='y'|| saveChoice =='Y')
saveData(players, SIZE);
}
break;
default:
cout << "Invalid choice" << endl;
}
} while (choice !=99);
return 0;
}
void inputData(Player players[], int size)
{
ifstream inFile("Ch9_Ex7Data.txt");
if (!inFile)
{
cerr << "Error opening input file." << endl;
exit(1); // Exit with an error code
}
for (int i =0; i < size; i++)
{
inFile >> players[i].name;
inFile >> players[i].position;
inFile >> players[i].touchDowns;
inFile >> players[i].catches;
inFile >> players[i].passingYards;
inFile >> players[i].receivingYards;
inFile >> players[i].rushingYards;
}
inFile.close();
}
void outputData(Player players[], int size)
{
for (int i =0; i < size; i++)
{
cout << "Name: "<< players[i].name << endl;
cout << "Position: "<< players[i].position << endl;
cout << "Touch Downs: "<< players[i].touchDowns << endl;
cout << "Number of Catches: "<< players[i].catches << endl;
cout << "Passing Yards: "<< players[i].passingYards << endl;
cout << "Receiving Yards: "<< players[i].receivingYards << endl;
int search(Player players[],

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions