ok need a bit of help.

got a program to set up football league table and input all the scores etc etc

just tryin to figure a way to add scores and that from there, also the display code seems a bit messy anyway i can put it into a incremental loop?

well the code is....

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 8
#define TRUE 1
#define FALSE 0

void prior_insertion(void);

void draw_table (void);
void insertion_sort (void);
void draw_sorted (void);
void gfor_against (void);
void sort_alpha (void);


struct
{
	char team[20];
	int played;
	int points;
	int goalsf;
	int goalsa;
	int goalsd;
}arrdetails[8];

main()
{
prior_insertion();
draw_table();
insertion_sort();
gfor_against();
sort_alpha();
draw_sorted ();

}

void prior_insertion(void)

{
	/*teams*/
    	strcpy(arrdetails[0].team,"Stoke City");
    	strcpy(arrdetails[1].team,"Manchester City");
    	strcpy(arrdetails[2].team,"Port Vale");
    	strcpy(arrdetails[3].team,"Derby County");
    	strcpy(arrdetails[4].team,"Birmingham Town");
    	strcpy(arrdetails[5].team,"Crystal Palace");
    	strcpy(arrdetails[6].team,"Luton");
    	strcpy(arrdetails[7].team,"Hull City");

  	/*played*/
  	arrdetails[0].played=3;
	arrdetails[1].played=3;
	arrdetails[2].played=3;
	arrdetails[3].played=3;
	arrdetails[4].played=3;
	arrdetails[5].played=3;
	arrdetails[6].played=3;
	arrdetails[7].played=3;

	/*points*/
	arrdetails[0].points=5;
	arrdetails[1].points=3;
	arrdetails[2].points=1;
	arrdetails[3].points=9;
	arrdetails[4].points=7;
	arrdetails[5].points=5;
	arrdetails[6].points=1;
	arrdetails[7].points=1;

	/*goalsfor*/
	arrdetails[0].goalsf=4;
	arrdetails[1].goalsf=5;
	arrdetails[2].goalsf=2;
	arrdetails[3].goalsf=7;
	arrdetails[4].goalsf=4;
	arrdetails[5].goalsf=4;
	arrdetails[6].goalsf=0;
	arrdetails[7].goalsf=0;

	/*goalsagainst*/
        arrdetails[0].goalsa=3;
	arrdetails[1].goalsa=6;
	arrdetails[2].goalsa=6;
	arrdetails[3].goalsa=2;
	arrdetails[4].goalsa=2;
	arrdetails[5].goalsa=3;
	arrdetails[6].goalsa=2;
	arrdetails[7].goalsa=2;
}


void draw_table (void)
{
	printf("BELOW IS BEFORE AN INSERTION SORT\n");
	printf("\t\t\tPlayed\tPoints\tGoals for\tGoals against\n");
	printf("\n");
	printf("%s \t\t %d \t %d \t %d \t \t %d\n",arrdetails[0].team,arrdetails[0].played,arrdetails[0].points,arrdetails[0].goalsf,arrdetails[0].goalsa);
	printf("%s \t %d \t %d \t %d \t \t %d\n",arrdetails[1].team,arrdetails[1].played,arrdetails[1].points,arrdetails[1].goalsf,arrdetails[1].goalsa);
	printf("%s \t\t %d \t %d \t %d \t \t %d\n",arrdetails[2].team,arrdetails[2].played,arrdetails[2].points,arrdetails[2].goalsf,arrdetails[2].goalsa);
	printf("%s \t\t %d \t %d \t %d \t \t %d\n",arrdetails[3].team,arrdetails[3].played,arrdetails[3].points,arrdetails[3].goalsf,arrdetails[3].goalsa);
	printf("%s \t %d \t %d \t %d \t \t %d\n",arrdetails[4].team,arrdetails[4].played,arrdetails[4].points,arrdetails[4].goalsf,arrdetails[4].goalsa);
	printf("%s \t\t %d \t %d \t %d \t \t %d\n",arrdetails[5].team,arrdetails[5].played,arrdetails[5].points,arrdetails[5].goalsf,arrdetails[5].goalsa);
	printf("%s \t\t\t %d \t %d \t %d \t \t %d\n",arrdetails[6].team,arrdetails[6].played,arrdetails[6].points,arrdetails[6].goalsf,arrdetails[6].goalsa);
	printf("%s \t\t %d \t %d \t %d \t \t %d\n",arrdetails[7].team,arrdetails[7].played,arrdetails[7].points,arrdetails[7].goalsf,arrdetails[7].goalsa);
}

void insertion_sort (void)
{
	int pinsert;
 	int temp;
 	int z,i,in,gap;
 	int value;
	char tmpteam[20];
	int tmpplayed;
	int tmppoints;
	int tmpgoalsf;
	int tmpgoalsa;
	z = 0;

 	value=0;
	
	for (z = 0; z < MAX; z++)
	{
		while (arrdetails[z].points > arrdetails[z + 1].points)
		{
            	strcpy(tmpteam,arrdetails[z+1].team);
     	    	tmpplayed=arrdetails[z+1].played;
            	tmppoints=arrdetails[z+1].points; 
            	tmpgoalsf=arrdetails[z+1].goalsf; 
            	tmpgoalsa=arrdetails[z+1].goalsa;
            
            	strcpy(arrdetails[z+1].team,arrdetails[z].team);
            	arrdetails[z+1].played=arrdetails[z].played;
            	arrdetails[z+1].points=arrdetails[z].points;
            	arrdetails[z+1].goalsf=arrdetails[z].goalsf;
            	arrdetails[z+1].goalsa=arrdetails[z].goalsa;
            
            	strcpy(arrdetails[z].team,tmpteam);
            	arrdetails[z].played=tmpplayed;
            	arrdetails[z].points=tmppoints;
            	arrdetails[z].goalsf=tmpgoalsf;
            	arrdetails[z].goalsa=tmpgoalsa;
           		
             	z--;
  		}
	}
}

void gfor_against (void) 
{
 	int value,z,i,gap,in,x;
  	char tmpteam[20];
  	int tmpplayed;
  	int tmppoints;
  	int tmpgoalsf;
  	int tmpgoalsa;
  	int alpha;
	x = 0;
  
  	for (z = 0; z < MAX; z++)/*work out the goals difference*/
	{	
       		arrdetails[z].goalsd=arrdetails[z].goalsf-arrdetails[z].goalsa;
	}

   	for (z = 0; z < MAX; z++)
	{
		if(arrdetails[z].points == arrdetails[z+1].points)
     		{
			while (arrdetails[z].goalsd > arrdetails[z + 1].goalsd)
			{
				strcpy(tmpteam,arrdetails[z+1].team);
     	    			tmpplayed=arrdetails[z+1].played;
            			tmppoints=arrdetails[z+1].points; 
            			tmpgoalsf=arrdetails[z+1].goalsf; 
            			tmpgoalsa=arrdetails[z+1].goalsa;
            
            			strcpy(arrdetails[z+1].team,arrdetails[z].team);
            			arrdetails[z+1].played=arrdetails[z].played;
            			arrdetails[z+1].points=arrdetails[z].points;
            			arrdetails[z+1].goalsf=arrdetails[z].goalsf;
            			arrdetails[z+1].goalsa=arrdetails[z].goalsa;
            
            			strcpy(arrdetails[z].team,tmpteam);
            			arrdetails[z].played=tmpplayed;
            			arrdetails[z].points=tmppoints;
            			arrdetails[z].goalsf=tmpgoalsf;
            			arrdetails[z].goalsa=tmpgoalsa;
            			
				z--;
			}
		}				
	}
}
  

void sort_alpha  (void)
{
	int z;
	int j;
	char tmpteam[20];
  	int tmpplayed;
  	int tmppoints;
  	int tmpgoalsf;
  	int tmpgoalsa; 
	int i ;
		
		if (strcmp(arrdetails[z].team, arrdetails[z+1].team ))
     		{
			i = -1;
			strcpy(tmpteam,arrdetails[z+1].team);
     	    		tmpplayed=arrdetails[z+1].played;
            		tmppoints=arrdetails[z+1].points; 
            		tmpgoalsf=arrdetails[z+1].goalsf; 
            		tmpgoalsa=arrdetails[z+1].goalsa;
            
            		strcpy(arrdetails[z+1].team,arrdetails[z].team);
            		arrdetails[z+1].played=arrdetails[z].played;
            		arrdetails[z+1].points=arrdetails[z].points;
            		arrdetails[z+1].goalsf=arrdetails[z].goalsf;
            		arrdetails[z+1].goalsa=arrdetails[z].goalsa;
            
            		strcpy(arrdetails[z].team, arrdetails[j].team);
            		arrdetails[z].played=arrdetails[j].played;
            		arrdetails[z].points=arrdetails[j].points;
            		arrdetails[z].goalsf=arrdetails[j].goalsf;
            		arrdetails[z].goalsa=arrdetails[j].goalsa;			
		}
		
		else
		{
			i = 1;			
		}




}

void draw_sorted (void)
{
        
	printf("\nAFTER INSERTION SORT\n"); 
   	printf("\t\t\tPlayed\tPoints\tGoals for\tGoals against\n");
	printf("\n");
	printf("%s \t %d \t %d \t %d \t \t %d\n",arrdetails[7].team,arrdetails[7].played,arrdetails[7].points,arrdetails[7].goalsf,arrdetails[7].goalsa);
	printf("%s \t\t %d \t %d \t %d \t \t %d\n",arrdetails[6].team,arrdetails[6].played,arrdetails[6].points,arrdetails[6].goalsf,arrdetails[6].goalsa);
	printf("%s \t\t %d \t %d \t %d \t \t %d\n",arrdetails[5].team,arrdetails[5].played,arrdetails[5].points,arrdetails[5].goalsf,arrdetails[5].goalsa);
	printf("%s \t %d \t %d \t %d \t \t %d\n",arrdetails[4].team,arrdetails[4].played,arrdetails[4].points,arrdetails[4].goalsf,arrdetails[4].goalsa);
	printf("%s \t\t %d \t %d \t %d \t \t %d\n",arrdetails[3].team,arrdetails[3].played,arrdetails[3].points,arrdetails[3].goalsf,arrdetails[3].goalsa);
	printf("%s \t\t\t %d \t %d \t %d \t \t %d\n",arrdetails[2].team,arrdetails[2].played,arrdetails[2].points,arrdetails[2].goalsf,arrdetails[2].goalsa);
	printf("%s \t\t %d \t %d \t %d \t \t %d\n",arrdetails[1].team,arrdetails[1].played,arrdetails[1].points,arrdetails[1].goalsf,arrdetails[1].goalsa);
	printf("%s \t\t\t %d \t %d \t %d \t \t %d\n",arrdetails[0].team,arrdetails[0].played,arrdetails[0].points,arrdetails[0].goalsf,arrdetails[0].goalsa);
}

thanks everyone

Wow!!! First post and you used CODE tags! Thank You. What is it you know that no other first-time poster can figure out? :mrgreen:

ok need a bit of help.

Good, that's what we're here for...

just tryin to figure a way to add scores and that from there,

Huh? What's that mean?

also the display code seems a bit messy anyway i can put it into a incremental loop?

I suppose...

The code is quite long (obviously) and you give no indication where you need changes. Maybe you need to post only the section of the code you need help with and a better explanation of exactly what you are looking for.

ok thanks for sayin i can use code tags aint hard reli if i can program in c and java :P

so yea that code inputs a football league table and then outputs it unsorted and then sorted.....
...
...
...what i need help with is how would i go about adding a way off adding more data

for example add a game played always one.
points either 0 for a loss, 1 for a draw or 3 for a win, goals scored and goals scored against

want to end up with a menu system and to be able to do all the options below....so far got B and D sorted out just taking things one step at a time

****************************
* Football League *
****************************
A. Add A New Team
B. Display Current Table
C. Enter Match Details
D. Exit System


thanks again

...what i need help with is how would i go about adding a way off adding more data

for example add a game played always one.
points either 0 for a loss, 1 for a draw or 3 for a win, goals scored and goals scored against

want to end up with a menu system and to be able to do all the options below....so far got B and D sorted out just taking things one step at a time

****************************
* Football League *
****************************
A. Add A New Team
B. Display Current Table
C. Enter Match Details
D. Exit System

Taking one step at a time is the best way to program...

Start with the menu. Put a while loop in main() that
a) displays the menu
b) accept the menu selection
c) executes the appropriate function
d) exits when the EXIT selection is chosen

To add data, do you have a way to store the data for each game won? That would be the first thing. What do you have to save? What are the permissible values? Once you have that, write a function to input the information and load the data.

so if i add sommate along the lines of

main()
{
 char option, rtn;
 option = print_header();
 while(option != 'q' && option != 'Q')
 {
    switch(option)
      {
    case 'a': case 'A': /*Enter team */
         printf("\nPlease Enter a new team:\n");
         break;
    case 'b': case'B': /*Display table*/
         printf("\nLeague Table:\n");
         break;
    case 'c': case'C': /*Match details*/
         printf("\nLeague Table:\n");
         break;
    case 'd': case 'D': /*Exit*/
         printf("\n*** FINISHED ***\n");
         exit(0);
    default: /*invalid option letter entererd*/
         printf("\nInvalid choice, Please try again\n");
     }
   option = print_header();
   }
  return 0;
}

sorry for being slow, just not realy good at C java im not soo bad at.

we are doing 3 programming languages this term, C, Java and VB and i dont get C at all. cnt wait till next year when im only doing java

networking is more my cup of tea but we have to do the general route in our 1st year

Close....

main()  /**** main is an INT ****/
{
 char option, rtn;
 option = print_header();
 while(option != 'q' && option != 'Q')
 {
/**** display your menu ****/
/****   accept option   ****/
    switch(option)
      {
    case 'a': case 'A': /*Enter team */
         printf("\nPlease Enter a new team:\n");
         break;
    case 'b': case'B': /*Display table*/
         printf("\nLeague Table:\n");
         break;
    case 'c': case'C': /*Match details*/
         printf("\nLeague Table:\n");
         break;
    case 'd': case 'D': /*Exit*/
         printf("\n*** FINISHED ***\n");
         exit(0);
    default: /*invalid option letter entererd*/
         printf("\nInvalid choice, Please try again\n");
     }
   option = print_header();
   }
  return 0;
}

In each case , call a function to process the selection.

so how would i go about pointing for example case C to the function

void draw_sorted (void)

so that when b is pressed the table it displayed??

I suppose you donno how to use scanf().
As you said you know java let me just say:
System.out.ptintln() = printf()
System.in.read() = scanf()
Here is a sample:

//Just gave a name to your struct..
struct TeamInfo
{
    char team[20];
    int played;
    int points;
    int goalsf;
    int goalsa;
    int goalsd;
} ;

#define MAX_ALLOWED_TEAMS 20
struct TeamInfo arrdetails[MAX_ALLOWED_TEAMS] ;
int current_empty_slot = 0 ;

void take_team_input() ;

main()  /**** main is an INT ****/
{
    //...........
    while(option != 'q' && option != 'Q')
    {
        //...........
        switch(option)
        {
            //...........
            case 'c': case'C': /*Take match details input*/
            //just call the function that does what you want in this case..
            take_team_input() ;
            break;
            //...........
        }
        option = print_header();
    }
    return 0;
}

void take_team_input()
{
    char choice = 'Y' ;

    if( MAX_ALLOWED_TEAMS <= current_empty_slot )
    {
        printf( "\nMax limit for team entries reached.", ) ;
        return ;
    }
    
    while( ('y' == choice) || ('Y' == choice) )
    {
        //take the input:
        printf("\nTaking input for team number: %d", current_empty_slot ) ;
        printf("\nEnter team name: ") ; fflush(null) ;
        scanf( "%s", arrdetails[current_empty_slot].team ) ;
        printf("\nEnter number of games played: ") ; fflush(null) ;
        scanf( "%d", arrdetails[current_empty_slot].played ) ;
        printf("\nEnter goals for: ") ; fflush(null) ;
        scanf( "%d", arrdetails[current_empty_slot].goalsf ) ;
        //...... and so on...
        //now ask if he wants to enter any more...
        printf("\n\nWant to enter details for any more teams? [y/n]: ") ; fflush(null) ;
        scanf( "%c", choice ) ;

        current_empty_slot++ :
    }
}

"fflush(null) ;" isn't really mandatory, but lotsa ppl come back quoting problems with printf+scanf usage so...

"fflush(null) ;" isn't really mandatory, but lotsa ppl come back quoting problems with printf+scanf usage so...

Even more ppl come back yelling at anyone that uses it improperly like this, too. fflush(null); will accomplish nothing at best, break your program at worst. You cannot flush the null device AFAIK. And it is only used for output, never input, streams. See this

start-quote

void take_team_input()
{
    char choice = 'Y' ;

    if( MAX_ALLOWED_TEAMS <= current_empty_slot )
    {
        printf( "\nMax limit for team entries reached.", ) ;
        return ;
    }

    while( ('y' == choice) || ('Y' == choice) )
    {
        //take the input:
        printf("\nTaking input for team number: %d", current_empty_slot ) ;
        printf("\nEnter team name: ") ; fflush(null) ;
        scanf( "%s", arrdetails[current_empty_slot].team ) ;
        printf("\nEnter number of games played: ") ; fflush(null) ;
        scanf( "%d", arrdetails[current_empty_slot].played ) ;
        printf("\nEnter goals for: ") ; fflush(null) ;
        scanf( "%d", arrdetails[current_empty_slot].goalsf ) ;
        //...... and so on...
        //now ask if he wants to enter any more...
        printf("\n\nWant to enter details for any more teams? [y/n]: ") ; fflush(null) ;
        scanf( "%c", choice ) ;

        current_empty_slot++ :
    }
}

end-quote

within the while loop i added another if loop so that is checks the max number of teams to see if the limit has been reached. the first loop you had only checks the initial value which is always going to be zero, once ive entered all the teams details and go through add teams again it starts from zero because thats wat the initial value is always set 2.

in my code now ive removed the insersion peice because if im going to be able to add team into manually i dont need preset data.

also for now ive removed the sort untill ive got everything else sorted.

my current code is:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 8
#define TRUE 1
#define FALSE 0

char print_header(void);
void draw_table (void);

struct TeamInfo
{
    char team[20];
    int played;
    int points;
    int goalsf;
    int goalsa;
    int goalsd;
} ;

#define MAX_ALLOWED_TEAMS 20
struct TeamInfo arrdetails[MAX_ALLOWED_TEAMS] ;

void take_team_input();
void take_team_name();


/************************************************************/
/*                Main Function                             */
/************************************************************/
main()  /**** main is an INT ****/
{
 char option, rtn;
 option = print_header();
 while(option != 'q' && option != 'Q')
 {
/**** display your menu ****/
/****   accept option   ****/
    switch(option)
      {
    case 'a': case 'A': /*Enter team */
         printf("\nPlease Enter a new team:\n");
         take_team_name();
         break;
    case 'b': case'B': /*Display table*/
         printf("\nLeague Table:\n");
         draw_table();
         break;
    case 'c': case'C': /*Match details*/
            //just call the function that does what you want in this case..
            take_team_input();
            break;
    case 'd': case 'D': /*Exit*/
         printf("\n*** FINISHED ***\n");
         exit(0);
    default: /*invalid option letter entererd*/
         printf("\nInvalid choice, Please try again\n");
     }
   option = print_header();
   }
  return 0;
}

/****************************************************************/
/*                      Functions                               */
/****************************************************************/
/****************************************************************/
/*                      Print Menu Options                      */
/****************************************************************/
char print_header(void)
{
   char opt,rtn;
   printf("\n****************************");
   printf("\n*     Football League    *");
   printf("\n****************************\n");
   printf("\na.\tEnter a New Team\n");
   printf("\nb.\tDisplay Current Table\n");
   printf("\nc.\tEnter Match Details\n");
   printf("\nd.\tExit System\n");
   printf("\nPlease Enter Your Choice: \n");
   scanf("%c%c",&opt,&rtn);
   return opt;
}


/****************************************************************/
/*            Enters Team Name            */
/****************************************************************/
void take_team_name()
{
    int current_empty_slot = 0 ;
    char choice = 'Y' ;

    if(MAX_ALLOWED_TEAMS <= current_empty_slot)
    {
        printf("\nMax limit for team entries reached.", );
        return ;
    }

    while(('y' == choice) || ('Y' == choice))
    {
        //take the input:
        printf("\nTaking input for team number: %d", current_empty_slot );

        printf("\nEnter Team Name: ");
        fflush(stdout);
        scanf( "%s", arrdetails[current_empty_slot].team );

        //now ask if he wants to enter any more...
        printf("\n\nWant To Enter Anymore Team Names? [y/n]: ");
        fflush(stdout);
        scanf( "%c", choice );

    current_empty_slot++;

        if(MAX_ALLOWED_TEAMS <= current_empty_slot)
            {
                printf("\nMax limit for team entries reached.", );
                return ;
             }
    }

}

/****************************************************************/
/*            Enters Match Details            */
/****************************************************************/
void take_team_input()
{
    int current_empty_slot = 0 ;
    char choice = 'Y' ;

    if(MAX_ALLOWED_TEAMS <= current_empty_slot)
    {
        printf("\nMax limit for team entries reached.", );
        return ;
    }

    while(('y' == choice) || ('Y' == choice))
    {
        //take the input:
        printf("\nTaking input for team number: %d", current_empty_slot );

        printf("\nEnter team name: ");
        fflush(stdout);
        scanf( "%s", arrdetails[current_empty_slot].team );

        printf("\nEnter number of games played: ");
        fflush(stdout);
        scanf( "%d", arrdetails[current_empty_slot].played );

        printf("\nEnter goals for: ");
        fflush(stdout);
        scanf( "%d", arrdetails[current_empty_slot].goalsf );
        //...... and so on...
        //now ask if he wants to enter any more...
        printf("\n\nWant to enter details for any more teams? [y/n]: ");
        fflush(stdout);
        scanf( "%c", choice );

        current_empty_slot++;
    }

}

/****************************************************************/
/*                  Displays the current teams                  */
/*                 And Info                           */
/****************************************************************/
void draw_table (void)
{
    int i;
        // Displays Sorted League Table
    for ( i = 0; i < MAX; i++ )
        {
         printf("%20s \t %d \t %d \t %d \t \t %d\n",arrdetails[i].team,arrdetails[i].played,arrdetails[i].points,arrdetails[i].goalsf,arrdetails[i].goalsa);
        }
}

got a the problem mentioned above with the MAX_ALLOWED_TEAMS always being reset to zero when the add team name option is selected, also it only allows me to enter a single word if i enter more than one it messes up the whole thing.

also when adding the match details i need to enter the team to add the details for and instead of creating a new peice of data in the array i need it to add the data to the current data that is already there

thanks everyone for there help its much appreachiated and im starting to understand c alot more now and slowly making sum progress on my own even tho im still getting alot of help from you.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

got another quick thing

if ive got the code

printf("\nWin, Loss or Draw ");
        fflush(stdout);
        scanf( "%d", arrdetails[current_empty_slot].points );

if i wanted to input a string from the user and change Win into 3 points added to points, 0 point added for loss and 1 point added to draw would i just use a swich statement with each case being win loss and draw then just within the case add the relivent points depending on wat the user selects?

within the while loop i added another if loop so that is checks ...

it's an if statement. IF is not a loop... ;)

got a the problem mentioned above with the MAX_ALLOWED_TEAMS always being reset to zero when the add team name option is selected, also it only allows me to enter a single word if i enter more than one it messes up the whole thing.

MAX_ALLOWED_TEAMS cannot be reset to 0. That's impossible. The problem is your use of scanf() . This series will explain both what the problem is and how to fix it.


also when adding the match details i need to enter the team to add the details for and instead of creating a new peice of data in the array i need it to add the data to the current data that is already there

So you enter the team number, search for it, display the team name, and accept the new data, and load it into the structure.


Looking at your code:

main()  /**** main is an INT ****/

are you ignoring the comment? It's important!

This article has been dead for over six months. Start a new discussion instead.