Thread: please...help me n building football league.....i really really stuck...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    Lightbulb please...help me n building football league.....i really really stuck...

    this programming still not perfect....
    i'm actually want to create football fixtures and user can input how many team will play...then..they can see the table...
    follow the ranking based on their point...

    (it need to input all score every match so you can see the proper table if not some weird number appear)
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<windows.h>
    
    int main()
    {
    	int option,y,score,goalA,goalB,goalC,goalD,x,W1,W2,W3,W4,W5,W6,L1,L2,L3,L4,L5,L6,D1,D2,D3,D4,D5,D6;
    	char teamA[100], teamB[100],teamC[100],teamD[100];
    	int TeamAWin (int, int, int);
    	int TeamALose(int, int, int);
    	int TeamADraw(int, int, int);
    
    	int TeamBWin (int, int, int);
    	int TeamBLose(int, int, int);
    	int TeamBDraw(int, int, int);
    
    	int TeamCWin (int, int, int);
    	int TeamCLose(int, int, int);
    	int TeamCDraw(int, int, int);
    
    	int TeamDWin (int, int, int);
    	int TeamDLose(int, int, int);
    	int TeamDDraw(int, int, int);
    
    	for(y=0;y<1000;y++)
    	{
    	printf("Enter '1' to see the fixtures\nEnter '2' to see the table\nEnter '3' to create league\nEnter '4' to end\n");
    	scanf("%d",&option);
    	system("cls");
    
    	switch(option)
    	{
    		case 1:
    			printf("1 = %s vs %s\n",teamA,teamC);
    			printf("2 = %s vs %s\n",teamB,teamD);
    			printf("3 = %s vs %s\n",teamA,teamB);
    			printf("4 = %s vs %s\n",teamC,teamD);
    			printf("5 = %s vs %s\n",teamA,teamD);
    			printf("6 = %s vs %s\n\n",teamC,teamB);
    
    			for(x=0;x<1000;x++)
    			{
    			printf("\n\nEnter number match to input the score\nenter '7' to go main menu\n");
    			scanf("%d",&score);
    
    			switch(score)
    			{
    				case 1:
    					printf("%s vs %s\n",teamA,teamC);
    					scanf("%d %d",&goalA,&goalC);
    					fflush(stdin);
    
    					if(goalA>goalC)
    					{
    						W1=1;	L1=0;	D1=0;
    						
    					}
    					else if(goalA<goalC)
    					{
    						L1=1;	W1=0;	D1=0;
    					}
    					else if(goalA==goalC)
    					{
    						D1=1;	W1=0;	L1=0;
    					} 
    					
    					break;
    				case 2:
    					printf("%s vs %s\n",teamB,teamD);
    					scanf("%d %d",&goalB,&goalD);
    					fflush(stdin);
    
    					if(goalB>goalD)
    					{
    						W2=1;	L2=0;	D2=0;
    						
    					}
    					else if(goalB<goalD)
    					{
    						L2=1;	W2=0;	D2=0;
    					}
    					else if(goalB==goalD)
    					{
    						D2=1;	W2=0;	L2=0;
    					}
    					
    					break;
    				case 3:
    					printf("%s vs %s\n",teamA,teamB);
    					scanf("%d %d",&goalA,&goalB);
    					fflush(stdin);
    
    					if(goalA>goalB)
    					{
    						W3=1;	L3=0;	D3=0;
    						
    					}
    					else if(goalA<goalB)
    					{
    						L3=1;	W3=0;	D3=0;
    					}
    					else if(goalA==goalB)
    					{
    						D3=1;	W3=0;	L3=0;
    					} 
    
    					break;
    				case 4:
    					printf("%s vs %s\n",teamC,teamD);
    					scanf("%d %d",&goalC,&goalD);
    					fflush(stdin);
    
    					if(goalC>goalD)
    					{
    						W4=1;	L4=0;	D4=0;
    						
    					}
    					else if(goalA<goalC)
    					{
    						L4=1;	W4=0;	D4=0;
    					}
    					else if(goalA==goalC)
    					{
    						D4=1;	W4=0;	L4=0;
    					} 
    
    					break;
    				case 5:
    					printf("%s vs %s\n",teamA,teamD);
    					scanf("%d %d",&goalA,&goalD);
    					fflush(stdin);
    
    					if(goalA>goalD)
    					{
    						W5=1;	L5=0;	D5=0;
    						
    					}
    					else if(goalA<goalD)
    					{
    						L5=1;	W5=0;	D5=0;
    					}
    					else if(goalA==goalD)
    					{
    						D5=1;	W5=0;	L5=0;
    					} 
    
    					break;
    				case 6:
    					printf("%s vs %s\n",teamC,teamB);
    					scanf("%d %d",&goalC,&goalB);
    					fflush(stdin);
    
    					if(goalC>goalB)
    					{
    						W6=1;	L6=0;	D6=0;
    						
    					}
    					else if(goalC<goalB)
    					{
    						L6=1;	W6=0;	D6=0;
    					}
    					else if(goalC==goalB)
    					{
    						D6=1;	W6=0;	L6=0;
    					} 
    
    					break;
    				case 7:
    					system("cls");
    					break;
    					
    				default:
    					puts("\nwrong input");
    			}
    			if(score==7)
    				break;
    			}
    			break;
    		case 2:
    			fflush(stdin);
    			printf("Team \twin\tlose\tdraw\tgoal\tconceded\n");
    			printf("TeamA\t%5d\t%d\t%d\t%d\t%d\n",TeamAWin(W1, W3, W5), TeamALose(L1, L3, L5), TeamADraw(D1, D3, D5));
    			printf("TeamB\t%5d\t%d\t%d\t%d\t%d\n",TeamBWin(W2, W3, W6), TeamBLose(L2, L3, L6), TeamBDraw(D2, D3, D6));
    			printf("TeamC\t%5d\t%d\t%d\t%d\t%d\n",TeamCWin(W1, W4, W6), TeamCLose(L1, L4, L6), TeamCDraw(D1, D4, D6));
    			printf("TeamD\t%5d\t%d\t%d\t%d\t%d\n",TeamDWin(W2, W4, W5), TeamDLose(L2, L4, L5), TeamCDraw(D1, D4, D6));
    
    			break;
    		case 3:
    			fflush(stdin);
    			printf("input 4 name team:\n");
    			
    			gets(teamA);
    			fflush(stdin);
    			
    			gets(teamB);
    			fflush(stdin);
    			
    			gets(teamC);
    			fflush(stdin);
    			
    			gets(teamD);
    			fflush(stdin);
    			
    			system("cls");
    			break;
    		case 4:
    			break;
    		default:
    			puts("wrong input\n\n");
    			
    	}
    	if(option==4)
    		break;
    	}
    return 0;		
    }
    
    int TeamAWin(int W1,int W3,int W5)
    {
    	int Win;
    	Win = W1 + W3 + W5;
    	return Win;
    }
    int TeamALose(int L1,int L3,int L5)
    {
    	int Lose;
    	Lose = L1 + L3 + L5;
    	return Lose;
    }
    int TeamADraw(int D1,int D3,int D5)
    {
    	int Draw;
    	Draw = D1 + D3 + D5;
    	return Draw;
    }
    int TeamBWin(int W2,int W3,int W6)
    {
    	int Win;
    	Win = W2 + W3 + W6;
    	return Win;
    }
    int TeamBLose(int L2,int L3,int L6)
    {
    	int Lose;
    	Lose = L2 + L3 + L6;
    	return Lose;
    }
    int TeamBDraw(int D2,int D3,int D6)
    {
    	int Draw;
    	Draw = D2 + D3 + D6;
    	return Draw;
    }
    int TeamCWin(int W1,int W4,int W6)
    {
    	int Win;
    	Win = W1 + W4 + W6;
    	return Win;
    }
    int TeamCLose(int L1,int L4,int L6)
    {
    	int Lose;
    	Lose = L1 + L4 + L6;
    	return Lose;
    }
    int TeamCDraw(int D1,int D4,int D6)
    {
    	int Draw;
    	Draw = D1 + D4 + D6;
    	return Draw;
    }
    int TeamDWin(int W2,int W4,int W5)
    {
    	int Win;
    	Win = W2 + W4 + W5;
    	return Win;
    }
    int TeamDLose(int L2,int L4,int L5)
    {
    	int Lose;
    	Lose = L2 + L4 + L5;
    	return Lose;
    }
    int TeamDDraw(int D1,int D3,int D5)
    {
    	int Draw;
    	Draw = D1 + D3 + D5;
    	return Draw;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Everything that currently has a hard-coded limit, like the four goal variables, and the six d/l/w variables, and the six game input prompts, etc., will have to become arrays. If you're allowed C99, you can use VLA to declare those arrays after you've input how many teams there are. Input and computation will then have to be in loops rather than a hard-coded number of calls.

    You'll also have to rethink your structure, as your d/l/w variable conceit won't extend to more teams at all, nor will your functions. You'll have to compute the total wins, total losses, etc. for each team as the data is input.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Here's what you need:

    1) A file with the names of your teams. They don't need to be sorted. Newer teams are added *only* to the end of this teams file.

    2) Another file, this has only the scores of the games played. Newer scores are added
    only to the end of *the right row*.

    Note: Both files are written to only by your league program. Not notepad, and not by humans.

    When your league program starts, the data in these two files is read into two arrays. One is for the teams name (and is an array of char), the other is for scores (and is an array of integers).

    When the program is done loading it will produce a scorecard like this example, for 4 teams:

    Code:
    \\\\\\  | Kickers | Kings | Flyers | Jaguars |
    ----------------------------------------------
    Kickers | ---     | 2-0   | 3-2    | 4-1     |
    Kings   | 0-2     | ---   | 3-3    | 2-1     |
    Flyers  | 2-3     | 3-3   | ---    | 5-1     |
    Jaguars | 1-4     | 1-2   | 1-5    | ---     |



    This is the sample program that made this scorecard:

    Code:
    /* An example of a portion of a league program, by Adak
    Status: ok
    
    # Our team names:
    kickers 
    kings 
    flyers 
    jaguars
    
    # Our Data for the Scores Array:
    2 3 4
    0 3 2
    2 3 5
    1 1 1
    
    What we want:
    \\\\\\  | kickers | kings | flyers | jaguars |
    kickers | ---     | 2-0   | 3-2    | 4-1     |
    kings   | 0-2     | ---   | 3-3    | 2-1     |
    flyers  | 2-3     | 3-3   | ---    | 5-1     |
    jaguars | 1-4     | 1-2   | 1-5    | ---     |
    */
    
    #include <stdio.h>
    #include <string.h>
    
    #define Rows 4
    #define Cols 12
    #define NumberOfGames 4
    
    //void output(char teams[][], int scores[][]);
    void output(char teams[Rows][Cols], int scores[Rows][NumberOfGames]);
    //format and output 
    
    int main(void) {
       
       int c, r, i, goals1, goals2; 
       int scores[Rows][NumberOfGames] = {
       { -1, 2, 3, 4 },
       { 0, -1, 3, 2 },
       { 2, 3, -1, 5 },
       { 1, 1, 1, -1 }
       };
       char teams[Rows][Cols] = {
       { "Kickers" }, 
       { "Kings" },
       { "Flyers" }, 
       { "Jaguars" }
       };
       
       //verify input
       putchar('\n');
       for(r = 0; r < Rows; r++)  {
          printf("%s", teams[r]);
          for(c = 0; c < NumberOfGames - 1; c++)
             printf(" %d", scores[r][c]);
          putchar('\n');
       }
       output(teams, scores);
       printf("\n\n\t\t    Program Complete - Press Enter When Ready ");
       i = getchar(); i++;
       return 0;
    }
    void output(char teams[Rows][Cols], int scores[Rows][NumberOfGames]) {
       int i, j, namelen, maxlen, spaces;
    
       printf("\n\n\n");
       for(i = 0, maxlen = 0; i < Rows; i++) {     //get the length of the longest team name
          namelen = strlen(teams[i]);
          if(namelen > maxlen)
            maxlen = namelen;
       }
       //print the header
       for(i = 0; i < maxlen; i++)  //the slashes: backslashes are escape char's
          printf("%c", '\\');       //so we need two of them, to print one.
       printf("  ");
    
    
       for(i = 0; i < Rows; i++)  {      //format the team column names
          printf(" %s ", teams[i]);      //columns should be as wide as 
          namelen = strlen(teams[i]);    //the longest team name
          if(namelen < maxlen)  {
             spaces = maxlen - namelen;
             while(--spaces > 0)
                putchar(' ');
          }
       }
       printf("\n-------------------------------------------");
        
       //print the rows
       for(i = 0; i < Rows; i++)  {
          printf("\n%s", teams[i]);
          namelen = strlen(teams[i]);
          if(namelen < maxlen)  {
             spaces = maxlen - namelen;
             while(spaces--)
                putchar(' ');
          }
          printf("  | ");
          for(j = 0; j < NumberOfGames; j++) {
             if(scores[i][j] < 0) {
                printf("  ---  |");
                continue;
             }
             //this is the scorecard flip trick:
             printf("  %d-%d  |", scores[i][j], scores[j][i]);
          }
       }
    }
    To the above program, you'll need to add functions for adding data, reading & writing the data to files, and any on-screen editing you want.

    I'm sorry about all your work in your own program, but it is rigidly structured, and very inadequate for something like this.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    TQVM.....!!!

    but i hav some question from your table(fixtures and result table)...
    how to input the score by asking the user...
    and from your table we can create the ranking table...


    thank u..

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by wetpipit View Post
    TQVM.....!!!

    but i hav some question from your table(fixtures and result table)...
    how to input the score by asking the user...
    and from your table we can create the ranking table...


    thank u..
    You'll want to add another function which:

    for(i = 0; i < 2; i++) //for each team in the game being added

    asks for the names of the first team and it's score in the game

    print the request for the team's name
    >> fgets() is good, scanf() is adequate to get the team's name

    search the team array until you find the matching name
    Use strcmp(team1, team2) == 0

    if(noMatch) is found
    >> add the new team to the end of the list

    print the request for that team's score in the game
    >> fgets() or scanf(), the number of goals they scored.

    then
    writes that data to the file in the right row and column

    and repeats the above in a loop, for the second team in this same game.


    To rank the teams by their wins, again add a function. ranking() perhaps
    ranking will have a very small struct:

    struct teams, with
    >> char name[25]
    >> int wins
    >> int losses
    >> int ties
    //anything else you want to track (from any other data file).
    }team;

    struct team[50] //array of 50 team structs

    Now just go through the teams and scores arrays, very similarly to what was done for the output() function, and add the names of each teams from the teams array, tally up the wins and losses for each team from the scores array, and put that data into your array of structs.

    Now sort the structs in this array[], according to their number of wins, in descending order. For this small a number, bubblesort will be faster than most other sorting algorithms:

    Code:
    for(i = 0; i < MaxNumberOfTeams - 1; i++)  {
       for(j = i + 1; j < MaxNumberOfTeams; j++)  {
          if(team[i].wins < team[j].wins)  {
             tempName = team[i].name;
             tempWins  = team[i].wins;
             tempLosses = team[i].losses;
             
             team[i].name = team[j].name;
             team[i].wins = team[j].wins;
             team[i].losses = team[j].losses;
    
             team[j].name = tempName;
             team[j].wins = tempWins;
             team[j].losses = tempLosses;
    
          }
       }
    } 
    
    /*Note that there is an easier way to sort this using an auxiliary arrray of pointers 
    or indexes, but it confuses beginners. If you have several members in the struct, you'll want to learn it though. 
    */
    Then you'll want to add showRanks() to print out the array of teams, which will be in ranked order. Don't use output().

    That should get you started.
    Last edited by Adak; 02-26-2009 at 08:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 06-08-2009, 05:33 PM
  2. problem with league table
    By vw1970 in forum C Programming
    Replies: 6
    Last Post: 01-04-2009, 09:16 AM
  3. Football team league need help to make this work
    By wurzull in forum C Programming
    Replies: 1
    Last Post: 04-07-2007, 07:35 AM
  4. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  5. Fantasy Football Registration
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-01-2002, 02:32 PM