Thread: problem with league table

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    7

    problem with league table

    i am new to c and really do not know where i have gone wrong. I have to make a football league table to show teams results and points.
    when i put the first results in it is fine but when you add the second set of results it all goes wrong.
    When it is a draw it also goes wrong so at the moment i have commented that out.
    I also cant seem to grasp how to make the teams increment up each time as at the moment they always seem to play each other.

    Code:
    #include <stdio.h>                             
    #include <string.h>
    #include <stdlib.h>
    
    #define MAXTEAMS 4							  
    
    
    
    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].teamScore > records[i+1].teamScore)
    		{
    			records[i].points += 2;
    			records[i].won ++;
    			records[i].played++;
    			records[i+1].lost++;
    			records[i+1].points -= 1; 
    			records[i+1].played++;
    			
    		} 
    		if (records[i+1].teamScore > records[i].teamScore)
    		{
    			records[i+1].points += 3;
    			records[i+1].won ++;
    			records[i+1].played++;
    			records[i].lost++;
    			records[i].points -= 1; 
    			records[i].played++;
    			
    		}
    		/*if (records[i].teamScore == records[i+1].teamScore)
    		{
    			records[i].points += 1;
    			records[i].won ++;
    			records[i].played++;
    			records[i+1].lost++;
    			records[i+1].points += 1; 
    			records[i+1].played++;
    		}*/
    	}
    
    	printf(" LeagueTable \n\n");
    	printf("**********************************************************\n\n");
    	printf("Team\tPlayed\tWon\tDrawn\tLost\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);
    	}
    }
    I've just removed all the comments so there may now be a couple of glitches.
    Any help as to where i am goin wrong would be very much appreciated.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    could you precise what you want to do with each function? because I don't get the logic: for instance in getTeam, I would expect the function to return an element of the team array, but it actually sets the name of each team, same thing for addTeam, the name suggests that the function adds an element, but in fact it resets the entire array of team...

    PS: you should get soon some appropriate advise about fflush(stdin)...
    Last edited by root4; 01-04-2009 at 03:30 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > fflush(stdin);
    Read the FAQ.

    > int team2Score;
    > int team1Points;
    > int team2Points;
    I think you need another structure to record an individual match (between two teams).

    You then iterate over all the matches to work out the win/loss totals for each team in the league.
    You then sort by "wins" to find out the team with the most wins.
    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.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    7
    i am supposed to be displaying all of the team names in a table, each team is meant to play each other, so i need to be able to add results. the table is meant to update so for each set of results it need to update, games played, won lost etc.

    i was told to always use fflusch after scanf to clear and excess data, is this wrong?

    as you can see i am having a few problems

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vw1970
    i am supposed to be displaying all of the team names in a table, each team is meant to play each other, so i need to be able to add results. the table is meant to update so for each set of results it need to update, games played, won lost etc.

    ...

    as you can see i am having a few problems
    As root4 pointed out, you need to be more clear on each part that you are trying to solve. Also, while it is good that you have broken your problem into functions, you should also work them one at a time, testing each of them individually.

    Quote Originally Posted by vw1970
    i was told to always use fflusch after scanf to clear and excess data, is this wrong?
    It is wrong to use fflush() on stdin since the behaviour of fflush() is undefined for input streams like stdin. Although it may be the case that fflush(stdin) "flushes" the input buffer, because of undefined behaviour anything can happen, e.g., you could launch a nuclear missle on yourself (special hardware required).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    7
    could you explain how i do more than 1 structure, how do i link them together to use them?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You may want to add a small char array, I'll call it hasPlayed[]. HasPlayed will have either a 1, an X, or a zero in each array element, and that will show us what team needs to still be played: (X is for their own team, which they can not play).

    This would be a part of each team's struct in Table[]

    Code:
    char hasPlayed[MaxTeams] = { 0 };  //set it initally to all zero's
    
    /*now looking at this small array, we'll see:
    
    X 0 0 0 //let's say there are four teams, for team 1, their hasPlayed[] array looks like this.
    
    Now team 1 plays team 4, and we want to not use the "Team 0" that C might like, so:
    
    X 0 0 1 //would show up in table.hasPlayed[0], and in table.hasPlayed[3] it would look like this:
    
    1 0 0 X;  //team 4 has played team 1
    
    When all the teams have been played the array would look like this, for team 2
    1 X 1 1.
    
    If you wanted to print out the whole season's play chart it would look like this:
    
    X 1 1 1  //team 1
    1 X 1 1  // 2
    1 1 X 1  // 3
    1 1 1 X  // 4
    So any team with a zero in it's has play array, still needs to play another game - and the team it should play is shown right in the hasPlayed[index].

    X 1 0 1 //shows that team 1 still hasn't played against team 2, since hasPlayed[2] = 0.

    You can replace the fflush(stdin), with either getchar(), or with gar = getchar(), if the first version of getchar gives your IDE a warning. I then add "gar++; to the last line before the end of the function, and that way I get a warning about "gar is a variable that is never used in function ... " . gar is just a variable for garbage values.

    I don't believe you can say that the home team will always be table[i], and the away team will always be at table[i + 1], and running that in a loop - oh that sounds absolutely painful!

    What about we have a function that takes two team numbers, and then you add the results in from the game, inside that function?

    void recordGame(int teamNumber1, int teamNumber2)

    Then teamNumber1 could be the home team, and teamNumber2 the away team, etc.

    Hope that helps.
    Last edited by Adak; 01-04-2009 at 09:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dictionary in C# to hash table in C?
    By dinoman in forum C Programming
    Replies: 2
    Last Post: 04-12-2009, 09:23 PM
  2. problem in multi times table
    By lolguy in forum C Programming
    Replies: 11
    Last Post: 12-28-2008, 11:17 AM
  3. Please help! Beginner stuck on table and array problem!
    By robsmith in forum C Programming
    Replies: 2
    Last Post: 03-10-2005, 11:42 AM
  4. Problem with Hash Table Linear Probing
    By kirby1024 in forum C Programming
    Replies: 5
    Last Post: 10-23-2002, 06:03 AM
  5. HELP:Hasing Table
    By Amber_liam in forum C++ Programming
    Replies: 2
    Last Post: 06-20-2002, 04:25 AM