C program for creating player list

This is a discussion on C program for creating player list within the C Programming forums, part of the General Programming Boards category; Define a structure called Cricket that will describe the following info: 1) Player Name 2) Team name 3) batting average ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    5

    Question C program for creating player list

    Define a structure called Cricket that will describe the following info:
    1) Player Name
    2) Team name
    3) batting average

    Using this info. declare an array 'Player' with 10 elements and write a program to read the information about all players. Create team wise list contaiing name of the player with their batting average.
    __________________________________________________ _

    i tried different books asked friends and seniors but no one was able to give solution.
    Tha actual problem is sorting names team wise.
    I tried with strcmp() and if(p.team_name=='India')
    But it didn't work.

    Please help me!!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_PLAYERS 500
    #define INPUT_FILE  "input.txt"
    
    typedef struct {
      char name[20];
      char team[20];
      int  batting_avg;
    } Cricket;
    
    int read_players(Cricket *list)
    {
      FILE *in;
      char *newline;
      int  i;
    
      if ((in = fopen(INPUT_FILE, "r")) == NULL)
        return -1;
      for (i = 0;; i++) {
        if (fgets(list[i].name, sizeof list[i].name, in) == NULL)
          break;
        if ((newline = strchr(list[i].name, '\n')) != NULL)
          *newline = '\0';
        if (fgets(list[i].team, sizeof list[i].team, in) == NULL)
          return -1;
        if ((newline = strchr(list[i].team, '\n')) != NULL)
          *newline = '\0';
        if (fscanf(in, "%d", &list[i].batting_avg) != 1)
          return -1;
        fgetc(in);
      }
      fclose(in);
    
      return i;
    }
    
    int cmp_name(const void *a, const void *b)
    {
      return strcmp(((Cricket*)a)->name, ((Cricket*)b)->name);
    }
    
    int cmp_team(const void *a, const void *b)
    {
      return strcmp(((Cricket*)a)->team, ((Cricket*)b)->team);
    }
    
    int cmp_avg(const void *a, const void *b)
    {
      if (((Cricket*)a)->batting_avg > ((Cricket*)b)->batting_avg)
        return -1;
      else if (((Cricket*)a)->batting_avg < ((Cricket*)b)->batting_avg)
        return 1;
      else
        return 0;
    }
    
    int main(void)
    {
      Cricket Player[MAX_PLAYERS];
      int    n;
      int    i;
    
      if ((n = read_players(Player)) != -1) {
        puts("SORT BY NAME");
        qsort(Player, n, sizeof Player[0], cmp_name);
        for (i = 0; i < n; i++) {
          printf("Player: %s\nTeam: %s\nBatting AVG: %d\n\n",
            Player[i].name, Player[i].team, Player[i].batting_avg);
        }
        puts("SORT BY TEAM");
        qsort(Player, n, sizeof Player[0], cmp_team);
        for (i = 0; i < n; i++) {
          printf("Player: %s\nTeam: %s\nBatting AVG: %d\n\n",
            Player[i].name, Player[i].team, Player[i].batting_avg);
        }
        puts("SORT BY BATTING AVG");
        qsort(Player, n, sizeof Player[0], cmp_avg);
        for (i = 0; i < n; i++) {
          printf("Player: %s\nTeam: %s\nBatting AVG: %d\n\n",
            Player[i].name, Player[i].team, Player[i].batting_avg);
        }
      }
    
      return 0;
    }
    My best code is written with the delete key.

  3. #3
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Prelude, your from the US right? And I thought a Cricket question would have been the only thing you couldn't answer correctly, guess I was wrong!
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    5
    Dear Prelude,

    Thanks for your code.
    But unfortunatelly the code didn't work.
    It gives output as :
    SORT BY NAME
    SORT BY TEAM
    SORT BY BATTING AVG

    it is not accepting data from user.
    Please check this problem.
    I need this program.
    Please help me.

    Thanks again for the code.

    SWapnil

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >But unfortunatelly the code didn't work.
    Then you didn't use it right, it works just fine for me.

    >it is not accepting data from user.
    It isn't supposed to. The program I posted reads from a file.

    >Please check this problem.
    >I need this program.
    >Please help me.
    This sounds a lot like you were expecting me to give you the the entire program so that you only had to turn it in. Since that would be wishful thinking, perhaps you should study the code I *did* give you and look for things that you can incorporate into your own program.
    My best code is written with the delete key.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>This sounds a lot like you were expecting me to give you the the entire program
    Unfortunately, that's what you're going to get when you post a whole loada code in response to a "please help me" request.

    Swapnil, we're here to help, show us you've made an attempt yourself, then people will be more willing to help you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Unfortunately, that's what you're going to get when you post a whole loada code in response to a "please help me" request.
    I'll be sure to tag a comment to all code cut/paste compilable then:
    Code:
    /*
     * Disclaimer:
     *   All unique code supplied in this post is given as-is.
     *   The author of this program is not responsible for any
     *   errors, warnings, inconsistencies, or general lack of
     *   usefulness concerning the actual problem in question.
     *
     *   The following code is given as an example only. Any
     *   resemblance to the complete solution of the given
     *   problem is purely coincidence.
     */
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    5

    Angry mind u

    Prelude,

    Hey!! I was not expecting these kind of messages from u!!!
    I have tried different ways to solve that program and can show you.
    I am not a kind of guy who will cut and paste readymade code and get marks in assignment!!
    I can sort that palyer team name list alphabatically and that program runs, but i had to choose different way of program as told by our professor!!
    Hey mind u !! u will be a great programmer but u have no right to make such statements!!!

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >u have no right to make such statements!!!
    Did a moderator add something that looked like a flame to my post when I wasn't looking? If I get reactions like this to polite posts then I won't bother taking the extra time to word my sentences so as not to insult anyone.

    >but i had to choose different way of program as told by our professor!!
    You didn't say how it was told by your professor, so I gave you the easiest solution. Be more specific and you'll get better help. And relax, you'll get an ulcer if you're always so high strung.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Sep 2003
    Posts
    5

    Talking code

    /*#include<stdio.h>
    #include<conio.h>
    #define MAX 3
    typedef struct cricket
    {
    char name[15];
    char team[15];
    float avg;
    }cricket;
    void input(cricket *);
    void print(cricket *);
    void main()
    {
    cricket player[10],*temp;
    clrscr();
    input(player);
    print(player);
    getch();
    }
    void input(cricket * arr)
    {
    int i;
    for(i=0;i<MAX;i++)
    {
    printf("\n\n");
    printf("Enter the Player name %d :",i+1);
    gets(arr[i].name);
    fflush(stdin);
    printf("\nEnter the team %d :",i+1);
    gets(arr[i].team);
    fflush(stdin);
    printf("\nEnter the batting average %d :",i+1);
    scanf("%f",&(arr[i].avg));
    fflush(stdin);
    }
    }
    void print(cricket * arr)
    {
    int i,j,k=0,t=0;
    char temp1[4][15] = {0};
    printf("\t\n\n Players according to team: \n");
    for(i=0;i<MAX;i++)
    {
    k = 0;
    while( k < MAX-1 ) // check wether arr[i] is printed previously or not
    {
    if(!strcmp(arr[i].team,temp1[k]))
    break;
    k++;
    }
    if( k < MAX-1 )
    continue;
    printf("------------------------------------------------------------------\n");
    printf("\nTeam name: %s\n\n\n",arr[i].team);//if team is not printed prev.
    printf("------------------------------------------------------------------\n");
    strcpy(temp1[t],arr[i].team);
    t++;
    for(j=i;j<MAX;j++)
    if(!strcmp(arr[i].team,arr[j].team))
    {
    printf("\nPlayer name: ");
    puts(arr[j].name);
    printf("\n");
    printf("Batting average: ");
    printf("%3.2f\n",arr[j].avg);
    }
    }
    }
    */
    Code:
    cvc

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Obvious problems at a glance:

    >void main()
    Eew.

    >gets(arr[i].name);
    Eew!

    >fflush(stdin);
    *plonk*

    The three most hated constructs around here are all bundled up in this one little program. Why are they hated? Well, void main is wrong, main must return an integer. The two correct variations are:
    Code:
    int main()
    
    or
    
    int main(void)
    gets is just plain nasty, it opens all kinds of security holes and there's no way to make it safe. Use fgets instead. fflush is only defined for output streams, with input streams the behavior is unpredictable at best. And stdin is an input stream.
    My best code is written with the delete key.

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>The three most hated constructs
    ... and it's not even in code tags
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 08:01 AM
  3. link list
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2001, 04:41 AM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21