Write a program that declares a struct to store the data of a football player (p
ID: 3689151 • Letter: W
Question
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 update the data of a player. (You may assume that 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.
Explanation / Answer
Hi I have written all functionality. Please read main function to know how data should be in input file (to read successfully). You can add extra menu driven program in main according to your need.
#include<iostream>
#include<fstream>
using namespace std;
struct FootBallPlayer{
string name;
int position;
int touchdowns;
int catches;
int passingYards;
int receivingYards;
int rushingYards;
};
//function prototype
/* function to read data from file , returns total number of records read from file */
int readData(FootBallPlayer players[]);
/* function to serach by name , return s index of records. If not available, returns -1*/
int search(FootBallPlayer players[], string name, int size);
/* function to print records */
void printData(FootBallPlayer players[], int size);
/* function to write into file */
void writeData(FootBallPlayer players[], int size);
int main(){
// declaring array of FootBallPlayer of size 10
FootBallPlayer players[10];
cout<<"Reading from file: "<<endl;
cout<<"Please Make sure that - each line of file should contains following information separated by a space: (in order)"<<endl;
cout<<"(players name, players position, number of touchdowns, number of catches, number of passing yards,"<<
"number of receiving yards, and the number of rushing yards)"<<endl;
// reading data from file
int size = readData(players);
// printing data
printData(players, size);
//searching data
string name;
cout<<"Enter name to search: ";
cin>>name;
int index = search(players, name, size);
if(index == -1)
cout<<"Record not found ";
else
cout<<"Found at index: "<<index<<endl;
// writing to file
writeData(players, size);
return 0;
}
// function definations
int readData(FootBallPlayer players[]){
char fileName[20];
cout<<"Enter input filename: ";
cin>>fileName;
// opening file to read
ifstream read;
read.open(fileName);
int i=0;
//reading
while(read>>players[i].name>>players[i].position>>players[i].touchdowns>>players[i].catches>>
players[i].passingYards>>players[i].receivingYards>>players[i].rushingYards){
i++;
}
return i;
}
void printData(FootBallPlayer players[], int size){
for(int i=0; i<size; i++){
cout<<"Name: "<<players[i].name<<endl;
cout<<" players position: "<<players[i].position<<endl;
cout<<" number of touchdowns: "<<players[i].touchdowns<<endl;
cout<<" number of catches: "<<players[i].catches<<endl;
cout<<" number of passing yards: "<<players[i].passingYards<<endl;
cout<<" number of receiving yards: "<<players[i].receivingYards<<endl;
cout<<" number of rushing yards: "<<players[i].rushingYards<<endl;
cout<<endl;
}
}
void writeData(FootBallPlayer players[], int size){
char fileName[20];
cout<<"Enter output filename: ";
cin>>fileName;
// opening file to read
ofstream write;
write.open(fileName);
int i=0;
//reading
while(i<size){
write<<players[i].name<<players[i].position<<players[i].touchdowns<<players[i].catches<<
players[i].passingYards<<players[i].receivingYards<<players[i].rushingYards<<endl;
i++;
}
cout<<"Data written successfully"<<endl;
}
int search(FootBallPlayer players[], string name, int size){
for(int i=0; i<size; i++){
if(name.compare(players[i].name) == 0) // found
return i;
}
return -1; // not found
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.