60 368
Assignments Done
98,1%
Successfully Done
In April 2018

Answer to Question #48602 in C for sol

Question #48602
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 choices.
Expert's answer
#include <stdio.h>
#include <string.h>
 
typedef struct{
       char name[40];
       int homeruns;
       int hits;
} player;
 
void Read(player *players);
void Write(player *players);
void Search(player *players);
void Update(player *players);
 
int main(){
       player players[10];
       char menu;
       int i;
      
       for (i=0; i<10; i++){
             players[i].name[0]=0;
             players[i].homeruns=0;
             players[i].hits=0;
       }
      
       do{
             printf(" 1\tLoadfrom file\n 2\tSearch\n 3\tUpdate the data of a player\n 4\tShow the data of a
player\n 5\tSave data\n 0\tExit\n");
             fseek(stdin, 0, SEEK_END); scanf("%c", &menu); fseek(stdin, 0, SEEK_END);
            
             switch (menu)
             {
             case '1': //Load from file
                    Read(players);
                    break;
             case '2': //Search
                    Search(players);
                    break;
             case '3': //Update the data of a player
                    printf("Enter theplayer's index (0..9): ");
                    scanf("%d", &i); fseek(stdin, 0, SEEK_END);
                    if ((i>=0)&&(i<=9)){
                          Update(players+i);
                    }
                    else{
                          printf("The indexis incorrect\n\n");
                    }
                    break;
             case '4': //Show the data of a player
                    printf("Enter theplayer's index (0..9): ");
                    scanf("%d", &i);
                    if ((i>=0)&&(i<=9)){
                          printf("%d: %s, %d,%d\n\n", i,players[i].name, players[i].homeruns, players[i].hits);
                    }
                    else{
                          printf("The indexis incorrect\n\n");
                    }
                    break;
             case '5': //Save data
                    Write(players);
                    break;
             default:
                    break;
             }
       }while (menu!='0');
 
       return 0;
}
 
void Read(player *players){
       char name[200];
       FILE *fp;
       int i;
 
       printf("Enterfilename: ");
       scanf("%s", name);
       fp=fopen(name,"rb");
       if (fp!=NULL){
             i=fread(players, sizeof(player), 10, fp);
             printf("%d itemsare read\n\n", i);
             fclose(fp);
       }
       else{
             printf("Erroropenig file!\n\n");
       }
}
void Write(player *players){
       char name[200];
       FILE *fp;
       int i;
 
       printf("Enterfilename: ");
       scanf("%s", name);
       fp=fopen(name,"wb");
       if (fp!=NULL){
             i=fwrite(players, sizeof(player), 10, fp);
             printf("%d itemsare write\n\n", i);
             fclose(fp);
       }
       else{
             printf("Erroropenig file!\n\n");
       }
}
void Search(player *players){
       char name[40];
       int i, result=-1;
 
       printf("Enter nameof the player: ");
       gets(name);
      
       for (i=0; i<10; i++){
             if (strcmp(players[i].name, name)==0){
                    result=i;break;
             }
       }
 
       if (result>=0){
             printf("Index ofthe player: %d\n\n", result);
       }
       else{
             printf("The playerwas not found\n\n");
       }
}
void Update(player *players){
       printf("Enter aname [%s]: ", players->name);
       gets(players->name);
       printf("Enter anumber of home runs [%d]: ", players->homeruns);
       scanf("%d", &(players->homeruns));
       printf("Enter anumber of hits [%d]: ", players->hits);
       scanf("%d", &(players->hits));
       printf("\n");
}


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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions