Like Tree2Likes
  • 2 Post By Click_here

Example Program Help

This is a discussion on Example Program Help within the C Programming forums, part of the General Programming Boards category; Hello everyone, I came across this example program whilst reading my C programming dummies guide that i dont understand. I ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    5

    Example Program Help

    Hello everyone,

    I came across this example program whilst reading my C programming dummies guide that i dont understand.

    I was wondering if anyone could explain it to me

    I know roughly all of the terminology so all technical terms are fine its just im not sure what each bit does herefootball_league_c.txt

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    well tell us what parts you dont understand?
    Code:
    #include <stdio.h>
    #include <string.h>
    #define NOOFTEAMS 10
    #define STRINGLENGTH 30
    #define NOOFGAMES 90
    struct team
    {
     char teamName[STRINGLENGTH];
     int points;
     int noOfGames;
     int matchesPlayed[90];
    };
    struct leagueTable
    {
     int noOfTeams;
     struct team lgTbl[NOOFTEAMS];
    };
    struct game
    {
     char homeTeam[STRINGLENGTH];
     char awayTeam[STRINGLENGTH];
     int homeGoals;
     int awayGoals;
    };
    struct matchesTable
    {
     int noOfGames;
     struct game mtchTbl[NOOFGAMES];
    };
     
    int displayMenu(void);
    struct leagueTable initialise(struct leagueTable leagueTeam);
    struct leagueTable doAction(int option, struct leagueTable leagueTeam);
    struct matchesTable addMatch(struct matchesTable teamGame);
    void showHistory(struct matchesTable teamGame);
    void displayAll(struct leagueTable leagueTeam);
    int main(void)
    {
     struct leagueTable leagueTeam;
     int option;
     
     leagueTeam.noOfTeams = initialise(leagueTeam);
     while((option = displayMenu()) !=4)
     {
      leagueTeam = doAction(option, leagueTeam);
     }  
     
     return 0;
    }
    struct leagueTable initialise(struct leagueTable leagueTeam)
    {
     struct leagueTable leagueTeam;
     char teamName[NOOFTEAMS][STRINGLENGTH];
     int count;
     
     leagueTeam.noOfTeams = 0;
     
     for(count = 0; count < NOOFTEAMS; count++)
     {
      printf("Enter team name: ");
      gets(teamName[count]);
      fflush(stdin);
     };
     
     return leagueTeam;
    }
    int displayMenu(void)
    {
     int option;
     
     printf("\n\nFootball League Program\n\n");
     printf("1\tAdd Match\n");
     printf("2\tShow History\n");
     printf("3\tShow League\n");
     printf("4\tExit\n\n");
     printf("Select: ");
     while(scanf("%d", &option) !=1 || option < 1 || option > 4)
     {
      fflush(stdin);
      printf("ERROR - MUST be number 1 to 3: ");
     }
     fflush(stdin);
     
     return option;
    }
    struct leagueTable doAction(int option, struct leagueTable leagueTeam)
    {
     switch(option)
     {
      case 1 : leagueTeam = addMatch(leagueTeam);
      break;
      case 2 : showHistory(leagueTeam);
      break;
      case 3 : displayAll(leagueTeam);
      break;
     }
     return leagueTeam;
    }
    
    struct matchesTable addMatch(struct matchesTable teamGame)
    {
     struct team leagueTeam
     
     printf("Enter home team name: ");
     gets(teamGame.mtchTbl[teamGame.noOfGames].homeTeam);
     fflush(stdin);
     printf("Enter away team name: ");
     gets(teamGame.mtchTbl[teamGame.noOfGames].awayTeam);
     fflush(stdin);
     printf("Enter home team goals scored: ");
     scanf("%d", &teamGame.mtchTbl[teamGame.noOfGames].homeGoals);
     fflush(stdin);
     printf("Enter away team goals scored: ");
     scanf("%d", &teamGame.mtchTbl[teamGame.noOfGames].awayGoals);
     fflush(stdin);
     
     if(teamGame.mtchTbl[teamGame.noOfGames].homeGoals > teamGame.mtchTbl[teamGame.noOfGames].awayGoals)
     {
      leagueTeam.lgTbl[leagueTeam.noOfTeams].points + 2;
     }
     else if(teamGame.mtchTbl[teamGame.noOfGames].homeGoals < teamGame.mtchTbl[teamGame.noOfGames].awayGoals)
     {
      leagueTeam.lgTbl[leagueTeam.noOfTeams].points - 1;
     }
     else if(teamGame.mtchTbl[teamGame.noOfGames].homeGoals == teamGame.mtchTbl[teamGame.noOfGames].awayGoals)
     {
      leagueTeam.lgTbl[leagueTeam.noOfTeams].points + 1;
     }
     else 
     {
      (printf"work out how to show points +3");
     }
     
     
     teamGame.noOfGames++;
     
     return teamGame;
    }
    void showHistory(struct matchesTable teamGame)
    {
     int indx;
     
     printf("Enter team name for history search: ");
     gets(teamGame.mtchTbl[teamGame.noOfGames].homeTeam);
     
     for(indx = 0; indx < teamGame.noOfGames; indx++)
     {
      printf("\nAway Team: %s", teamGame.mtchTbl[teamGame.noOfGames].awayTeam);
      printf("\nHome goals scored %d", &teamGame.mtchTbl[teamGame.noOfGames].homeGoals);
      printf("\nAway goals scored %d", teamGame.mtchTbl[teamGame.noOfGames].awayGoals);
     }  
    }
    void displayAll(struct leagueTable leagueTeam)
    { 
     int indx;
     
     for(indx = 0; indx < leagueTeam.noOfTeams; indx++)
     {
      printf("\n%s", leagueTeam.lgTbl[indx].teamName);
      printf("\n%d", leagueTeam.lgTbl[indx].noOfGames);
      printf("\n%d", leagueTeam.lgTbl[indx].points);
     }
    }

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    The sample program is probably not a good idea to study -

    It uses fflush(stdin)
    FAQ > Why fflush(stdin) is wrong - Cprogramming.com

    and gets()
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
    Salem and AndiPersti like this.
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    5
    I understand the fact there is a struct for team and i understand how the menu works but I really dont get why there is a struct for league and why there is a struct for matchestable, also I dont see why matches played has an array of 90 when i think that should be in noofgames. I dont get what the purpose of the functions are like what are they passing/returning? Basically I am confused by the whole thing

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-10-2012, 11:25 PM
  2. Replies: 6
    Last Post: 07-14-2012, 09:43 AM
  3. Replies: 1
    Last Post: 03-03-2009, 03:47 PM
  4. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  5. Replies: 18
    Last Post: 11-13-2006, 12:11 PM

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