Answer to Question #235668 in C++ for 0949

Question #235668

Write a program that declares a struct to store the data of a baseball player (player’s name, number of home runs, and number of hits). Declare an array of 10 components to store the data of 10 baseball 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 choice


1
Expert's answer
2021-09-10T18:53:22-0400
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


struct Players
{
	 
	
	char name[20];
	int home_runs;
	int number_hits;




};
void displayMenu();
int index(struct Players s[], int size);
void outputData(struct Players st[], int size);
int main(){
	struct Players pl[10]; 
   fstream file;
   
   file.open("tpoint.txt",ios::in); 
  for(int i=0; i<10; i++){
  	file>>pl[i].name>>pl[i].home_runs>>pl[i].number_hits;
  }
  
  displayMenu();
  int choice;
  cin>>choice;
  if(choice==1){
  	outputData(pl, 10);
  }
  else if(choice==2){
  	cout<<"The player is at position "<<index(pl, 10) + 1<<endl;
  }
  else if(choice==3){
int n = index(pl,10);
  
  int hom, run;
 
  
  cout<<"Enter the player number of home runs\n";
  cin>>hom;
  cout<<"Enter the player number of hits\n";
  cin>>run;
  
  
  
   fstream file1;
   
   file1.open("tpoint.txt",ios::out); 
   for(int i=0; i<10; i++){
   	if(i==n){
   			file1<<pl[i].name<<"  "<<hom<<"  "<<run<<endl;
	   }
	   else{
	   	file1<<pl[i].name<<"  "<<pl[i].home_runs<<"  "<<pl[i].number_hits<<endl;
	   }
   	
   }
  }
  
  
}




void outputData(struct Players st[], int size){
cout<<"Displaying information\n";
cout<<"Player Name\t  Number of home runs\t  Number of hits\n";  
for(int i=0; i<size; i++){
	cout<<st[i].name<<"\t\t\t"<<st[i].home_runs<<"\t\t\t"<<st[i].number_hits<<endl;
}
	
}


int index(struct Players p[], int s)
{
	string name1; 
   cout << "Enter name of player to update: ";
	cin >> name1;
	
	for (int i = 0; i < s; i++)    
	{
		if (p[i].name == name1)   
		{
			return i;
		}
	}
	return -1;
}


void displayMenu()
{
	system("CLS");
	cout << "Select an option to perform" << "\n\n1. Display Information \n2. Search a certain Player";
	cout << "\n3. Update player Information and ";
	cout << "Save the information to a file: ";
	cout << "\n0. Exit" << endl;
	
	
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS