league table

This is a discussion on league table within the C Programming forums, part of the General Programming Boards category; hi. This league table has one problem with it! it WILL give 3 points to the HOME team ONLY after ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    99

    Post league table

    hi. This league table has one problem with it! it WILL give 3 points to the HOME team ONLY after every match. i want it to work so that if both teams draw, each team gets one point and gets marked down as a draw on both parts and SECONDLY that if the away team WINS, it gets 3 points and the home team gets no points and the home team gets marked down as a loss and the away team gets marked down as a win. PLEASE HELP: HOW WOULD I ADJUST THE TABLE TO MAKE THESE THINGS HAPPEN.

    Code:
    #include <stdio.h>                             
    #include <string.h>
    #include <stdlib.h>
     
    #define MAXTEAMS 20                            
     
     
     
    struct league{                                          
        char teamName[50];
        char name[50];                       
        int teamScore;
        int team2Score;
        int totNoTeams;
        int points;
        int team1Points;
        int team2Points;
        int goalsFor;
        int goalsAgainst;
        int played;
        int won;
        int lost;
        int drawn;
    };
     
    void displayLeagueTable(struct league records[]);
    void addTeam(struct league records[]);
    void getTeam(struct league records[]);         
    void addResults(struct league records[]);     
    char displayMenu(void);                            
     
     
     
     
     
    int main(void)                                     
    {
         
        struct league table[MAXTEAMS];              
        char option;
     
            do
        {
            option = displayMenu();
     
            switch(option)                         
            {
            case 'a': getTeam(table),addTeam(table);
                    break;
            case 'b': addResults(table);
                    break;
            case 'c': displayLeagueTable(table);
                    break;
            case 'd': //calculateResult(table);
                    break;
            case 'e': break;
            }
        }while(option != 'e');
         
        return 0;
    }
     
    char displayMenu(void)
    {
        char selection = 'X';
     
        printf("a\tAdd New Team\n\n");          
        printf("b\tAdd Match Results\n\n");
        printf("c\tDisplay League Table\n\n");
        printf("d\tcalculate Result\n\n");
        printf("e\tExit\n\n");
     
        printf("\nSelection");              
        scanf("%c", &selection);
        fflush(stdin);
     
        while(selection != 'a' && selection != 'b' && selection != 'c'
                && selection != 'd' && selection != 'e')
        {
            printf("\n ERROR, Select choices between a-g: ");
            scanf("%c", &selection);
            fflush(stdin);
        }
     
        return selection;
    }
     
     
     
    void getTeam(struct league records[])   
    {
        int i;
        for (i=0; i<MAXTEAMS; i++)
        {
            printf("Add new Team: \t\n\n");
            scanf("%s", &records[i].name);
            fflush(stdin);
        }
    }
     
    void addTeam(struct league records[])
    {
        int i;
         
        for (i=0; i<MAXTEAMS; i++)
        {
         
            records[i].teamScore = 0;
            records[i].team2Score = 0;
            records[i].points = 0;
            records[i].team2Points = 0;
            records[i].goalsFor = 0;
            records[i].goalsAgainst = 0;
            records[i].played = 0;
            records[i].won = 0;
            records[i].lost = 0;
            records[i].drawn = 0;
             
        }
    }
     
     
    void addResults(struct league records[])
    {
     
        int i;
     
        for ( i = 0; i < MAXTEAMS-1; i++)
     
        {
            printf("Home team is:  %s\n\n", records[i].name);
     
            printf("Enter Home teams score:\n");
            scanf("%d", &records[i].teamScore);
            fflush(stdin);
     
            printf("Away team is: %s \n\n", records[i+1].name);
     
            printf("Enter Away teams score:\n");
            scanf("%d", &records[i+1].teamScore, i++);
            fflush(stdin);  
     
        if (strcmp(records[i+1].name, (records[i].name)) == 0)
        printf("Sorry teams Cannot play each other");
        }       
    }
     
     
    void displayLeagueTable(struct league records[])
    {
        int i;
     
        for ( i = 0; i < MAXTEAMS-1; i++)
        {
        if (records[i+1].teamScore > records[i].teamScore)
            {
                records[i+1].points += 3;
                records[i+1].won ++;
                records[i+1].played;
                records[i].won ;
                records[i].lost++;
                records[i].points =0 ; 
                records[i].played++;
                 
            }
            
          if (records[i].teamScore > records[i+1].teamScore)
            {
                records[i].points +=3;
                records[i].won ++;
                records[i].played++;
                records[i+1].lost++;
                records[i+1].points = 0; 
                records[i+1].played++;
                 
            } 
          
                 
            
            if (records[i].teamScore == records[i+1].teamScore)
            {
                records[i].points += 1;
                records[i].drawn ++;
                records[i].played++;
                records[i+1].drawn++;
                records[i+1].points += 1; 
                records[i+1].played++;
            }
        
     
        printf(" LeagueTable \n\n");
        printf("**********************************************************\n\n");
        printf("Team\t Played Won  Drawn    Lost\tFor\tAgst\tPoints\n\n\n");
        printf("***********************************************************\n\n");
         }
     
        for ( i = 0; i < MAXTEAMS; i++)
        {
            printf("%s \t%d \t%d \t%d \t%d \t%d \t%d \t%d\n", 
            records[i]. name, records[i].played, records[i].won,records[i].drawn, 
            records[i]. lost, records[i].goalsFor, records[i].goalsAgainst, 
            records[i].points);
        }
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,551
    You might have "ingenious" in your name, but copy/pasting code from an older post on the same site is about as dumb as it gets.
    problem with league table

    Try using your brain, as well as keys other than ctrl-c / ctrl-v if you want to make it as a programmer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    99

    thanks

    Quote Originally Posted by Salem View Post
    You might have "ingenious" in your name, but copy/pasting code from an older post on the same site is about as dumb as it gets.
    problem with league table

    Try using your brain, as well as keys other than ctrl-c / ctrl-v if you want to make it as a programmer.
    wow cheers for the help .. the program that i copied had results for a loss as -1, 2 for a win and OBVIOUSLY couldnt write a program correctly but thanks again for your amazing help

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Quote Originally Posted by ingeniousreader View Post
    wow cheers for the help .. the program that i copied had results for a loss as -1, 2 for a win and OBVIOUSLY couldnt write a program correctly but thanks again for your amazing help
    You don't want "help", you want someone to do the work and the thinking for you.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There must be a dozen football league programs out there with source code - maybe grab one that actually works?

    For this one, I'd suggest deleting the fflush(stdin), since that is undefined and probably won't work, and adding

    #define WIN 2
    #define LOSS 0
    #define TIE 1

    or whatever actual values you want. Then inside the program below that, replacing those "magic numbers" that are there now for wins, losses and ties, with the appropriate define word from above. That makes the program easier to understand AND much easier to change and troubleshoot for bugs.

    I believe my forum mates didn't catch the fact that you're not a programmer, and this is not you, just being lazy - you're asking an honest question.

    Give that a try, if you're able. If not, show a good example of typical input and the output you want (I don't need to see the wrong output at all, just what you want), and I'll spend a bit of time with it.

    I do remember this post, from a few years back. Crazy!

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    99

    cheers

    Quote Originally Posted by Adak View Post
    There must be a dozen football league programs out there with source code - maybe grab one that actually works?

    For this one, I'd suggest deleting the fflush(stdin), since that is undefined and probably won't work, and adding

    #define WIN 2
    #define LOSS 0
    #define TIE 1

    or whatever actual values you want. Then inside the program below that, replacing those "magic numbers" that are there now for wins, losses and ties, with the appropriate define word from above. That makes the program easier to understand AND much easier to change and troubleshoot for bugs.

    I believe my forum mates didn't catch the fact that you're not a programmer, and this is not you, just being lazy - you're asking an honest question.

    Give that a try, if you're able. If not, show a good example of typical input and the output you want (I don't need to see the wrong output at all, just what you want), and I'll spend a bit of time with it.

    I do remember this post, from a few years back. Crazy!
    thanks ADAK ill try that. im only in my first year of doing a degree and so ive only done half a year of programming so far so its no wonder that i cant pin point the problem but i WILL give the #define a try thanks for REAL HELP unlike others. and sorry you must be mistaken with this post because this is my first ever post on this site ha

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Gaming League Hiring
    By Absolute in forum Projects and Job Recruitment
    Replies: 4
    Last Post: 11-13-2011, 05:11 PM
  2. problem with league table
    By vw1970 in forum C Programming
    Replies: 6
    Last Post: 01-04-2009, 08:16 AM
  3. Making A League Table
    By apple_ranger in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:22 PM

Tags for this Thread


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