need help with C++ homework with structs. Can\'t use pointers, we didn\'t learn
ID: 3586134 • Letter: N
Question
need help with C++ homework with structs. Can't use pointers, we didn't learn those yet. Any help would be appreciated with this, thank you.
The program reads from a file and stores the data for ten baseball players, including player’s team, name of player, number of homeruns, batting average, and runs batted in. (You can make up your data, or get it online, for example: http://espn.go.com/mlb/statistics )
Write a program that declares a struct to store the data for a player. Declare an array of 10 components to store the data for 10 baseball players.
The program prints out a menu (in a loop, so this can be done again and again) giving the user a choice to:
print out all users and statistics
print out the statistics for a specific player
print out all data for a specific team
update the data for a particular player (change one of the statistics)
DO ALL WORK IN FUNCTIONS. USE A FUNCTION TO READ THE DATA, A FUNCTION TO PRINT THE MENU, and FUNCTIONS FOR EACH OF THE MENU OPTIONS.
Before the program terminates, give the user the option to store the data in an output file.
Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
#define SIZE 10;
struct player{ //structure of player
int id;
string team;
string name;
int homeruns;
int batting_avg;
int runs;
}s[SIZE]; //reference of array of structure
int main(){
int ch,choice,idp;
String teamName;
do{ //menu to be displayed
cout<<"enter your choice"<</n
<<"1. print out all users and statistics"<<
<<"2.print out the statistics for a specific player"<<
<<"3. print out all data for specific item"<<
<<"4.update the data for a particular player"<<
<<"5.store data in file"<<
<<"6.exit";
cin>>ch;
switch(ch){ //different options methods
case 1: printAll();
break;
case 2:
cout<<"enter id";
cin>>idp;
printSpecific(idp);
break;
case 3:
cout<<"enter team";
cin>>teamName;
printSpecificTeam(teamName);
break;
case 4:
cout<<"enter id and team you wnat to update";
cin>>idp>>teamName;
updatePlayer(idp,teamName);
break;
case 5: int k = storeData();
if(k==1)
cout<<"Successfully stored";
else
cout<<"not stored";
break;
case 6: exit(0);
break;
default : cout<<"invalid choice";
}
cout<<"want to continue?(y/Y)";
cin>>choice;
}
while(choice=='y'||choice=='Y');
}
void loadData(){ //read data from file
ifstream in("player.txt");
int recCount = 0;
while(!in.eof()&&recCount<SIZE){
if (!in)
{
cerr << "File can't be opened! " << endl;
system("PAUSE");
}
in>>s[recCount].id>>s[recCount].team>>s[recCount].name>>s[recCount].homeruns>>s[recCount].batting_avg>>s[recCount].runs;
++recCount;
}
}
void printAll(){ //print data after reading from file
loadData();
for (int i = 0; i < SIZE; i++)
{
cout << s[i].team << " ";
cout << s[i].name << " ";
cout << s[i].homeruns << " ";
cout << s[i].batting_avg << " ";
cout << s[i].runs << " " <<endl;
}
}
void printSpecific(int idp){ //print data of specific player
for(int i=0;i<SIZE;++i){
if(s[i].id==idp){
cout << s[i].team << " ";
cout << s[i].name << " ";
cout << s[i].homeruns << " ";
cout << s[i].batting_avg << " ";
cout << s[i].runs << " " <<endl;
break;
}
}
}
void printSpecificTeam(string teamName){ //print data of specific team
for(int i=0;i<SIZE;++i){
if(s[i].team==teamName){
cout << s[i].team << " ";
cout << s[i].name << " ";
cout << s[i].homeruns << " ";
cout << s[i].batting_avg << " ";
cout << s[i].runs << " " <<endl;
break;
}
}
}
void updatePlayer(int idp,String teamName){ //update particular player team by searching it through id
for(int i=0;i<SIZE;++i){
if(s[i].id==idp){
s[i].team=teamName;
break;
}
}
}
int storeData(){ //store data in file
ofstream out("player.txt");
if (fseek(out, 0, SEEK_SET) != 0)
{
printf("error! ");
return 0;
}
fwrite((struct player*)s, sizeof(struct player), 1, out);
return 1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.