Sorting an array alphabetically - split

This is a discussion on Sorting an array alphabetically - split within the C Programming forums, part of the General Programming Boards category; Code: // 2nd part footy.cpp : Defines the entry point for the console application. #include "stdafx.h" #include "string.h" #include "stdlib.h" ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    5

    football team need help to make this work very new to c

    Code:
    // 2nd part footy.cpp : Defines the entry point for the console application.
    
    
    #include "stdafx.h"
    #include "string.h"
    #include "stdlib.h"
    #define num_teams 4
    
    int menu();	// prototypes for functions to be called by main
    void enterteams();
    void enterresults();
    void printteams();
    void leagueTeams();
    //File*leagueTeams;
    typedef struct			
    { 
    	char name[4];
    	int played;
    	int won;
    }team;
    team Teams[num_teams];
    //once the array has been populated ,you can write the information to a file:
    voidwriteFiles()
    {
    	leagueTeams=fopen("c:\\C Assignment\\teams.dat","wb");
    	fwrite(Teams,sizeof(team),num_teams,leagueTeams);
    	fclose(leagueTeams);
    }
    void readFiles(team Teams[])
    {
    	leagueTeams=fopen("c:\\C Assignmemt\\teams.dat","rb");
    	if (leagueTeams == NULL)
    	{
    		printf("File cannot be opened\a\n");
    		exit(0);
    		{
    		fread(Teams,sizeof(team),num_teams,leagueTeams);
    		fclose(leagueTeams);
    		}
    {
    	char name[20];
    	int played;
    	int won;
    	int drawn;
    	int lost;
    	int goalsFor;
    	int goalsAgainst;
    	int totalPoints;
    	int take_team_input();
        int take_team_name();
        char hometeam[4],awayteam[4];
    
    	
    } team;				
    
    team teams[12];	
    
    
    {
    char hometeam[4],awayteam[4];
    int x,validteam;
    do
    {
    	validteam = 0;
    	printf("\nPlease enter the home team:");
    	gets(hometeam);
    
    	for(x = 0; x<num_teams;x++)
    	{
    		if(strcmp(hometeam,Teams[x].name) ==0)
    			validteam == 1;
    	}
    	if(!validteam)
    		printf("\nTeam name not valid,please re-enter\n");
    }while(!validteam);
    do
    {
    	validteam = 0;
    	printf("\nPlease enter the away team:");
    	gets(awayteam);
    
    	for(x = 0; x<num_teams;x++)
    	{
    		if(strcmp(awayteam,Teams[x].name) ==0)
    			validteam == 1;
    	}
    	if(!validteam)
    		printf("\nTeam name not valid,please re-enter\n");
    }while(!validteam);
    
    
    int main (void)			/* main calls menu() and this returns the option choice
    						   to the user.  Dependant on that choice, main() will call
    						   enterteams(), enterresults(),enterfixtures() or printteams(), or exit the program
    						   This will continue, once the three functions have finished
    						   their work until the user chooses option 4 - EXIT */
    {
    	int menuChoice;
    	do
    	{
    		menuChoice = menu();            // function is called and returns a value to menuChoice
    		if (menuChoice == 1)
    			enterteams();
    		if  (menuChoice == 2)
    		    enterresults();
    		else if (menuChoice == 3)
    		    printteams();
    	}while (menuChoice != 4);
    	system("pause");
    	return 0;
    }
    
    int menu()					           // offers choices to the user
    {
    	int choice;
    
    	system("cls");			           // clear screen
    
    	printf("\t\t\tMENU\n\t\t\t----\n\n");
    	printf("\t\t1.\tEnter Teams\n");
    	printf("\t\t2.\tEnter Results\n");
      	printf("\t\t3.\tDisplay current Table\n");
    	printf("\t\t4.\tExit\n\n");
    	printf("\t\tPlease enter choice: ");
    	scanf("%d", &choice);
    
    	return choice;
    }
    
    
    void enterteams()        //allows input of match fixtures
    {
    	
    	int x;
    	//char line[30];
    	system("cls");			// clear screen
    	for (x = 0; x < 4; x++)	
    	{
    		// scanf() only accepts one name-- so we use can use sscanf + gets()   
    		printf("Enter details for Teams %d\n\n",(x + 1)); 
    		printf("Name: ");
    		fflush(stdin);
    		gets(teams[x].name);		   
    		/*printf("Played: ");
    		scanf("%d", &teams[x].played);
    		printf("Won: ");
    		scanf("%d", &teams[x].won);
    		printf("Drawn: ");
    		scanf("%d", &teams[x].drawn);
    		printf("Lost: ");
    		scanf("%d", &teams[x].lost);
    		printf("Goals For: ");
    		scanf("%d", &teams[x].goalsFor);
    		printf("Goals Against: ");
    		scanf("%d", &teams[x].goalsAgainst);
    		printf("Total Points: ");
    		scanf("%d", &teams[x].totalPoints);
    		printf("Opposition: ");
    		scanf("%s", &teams[x].opposition);
    		printf("\n");*/
    
    	}
    }
    void enterresults()
    {
    	int x,y,hometeam,awayteam;
    	for (x = 0; x < 4; x++)
    		for (y =x+1; y <4; y++)
    		{
    			printf("Enter result for match between team %s and team %s: ", teams[x].name, teams[y].name);
    			scanf("%d %d", &hometeam, &awayteam);
    			teams[x].played++;
    			teams[y].played++;
    			teams[x].goalsFor = hometeam+teams[x].goalsFor;
    			teams[y].goalsFor= awayteam + teams[y].goalsFor;
    			teams[x].goalsAgainst = hometeam+teams[x].goalsAgainst;
    			teams[y].goalsAgainst = awayteam+ teams[y].goalsAgainst;
    			if ( hometeam == awayteam)
    			{
    				teams[x].totalPoints++;
    				teams[y].totalPoints++;
    			}
    			if  (hometeam>awayteam)
    			{
    				teams[x].totalPoints+1;
    			}
    		    if  (hometeam<awayteam)
    			{
    				teams[y].totalPoints+1;
    			}
    		}
    }
    
    void printteams()
    {
    int x;
    system("cls");			// clear screen	
    	
    	printf("|--------------------------------------------------------------------------------\\|\n");
    	printf("| %-20s | %2s | %2s | %2s | %2s | %2s | %2s | %7s |\n", "Name", "P", "W", "D", "L", "F", "A", "Points" );
    	printf("|--------------------------------------------------------------------------------\\|\n");
    
    	for (x = 0; x < 4; x++)
    	{
    	printf("| %-20s | %2d | %2d | %2d | %2d | %2d | %2d | %7d |\n",teams[x].name, teams[x].played, teams[x].won, teams[x].drawn, teams[x].lost, teams[x].goalsFor, teams[x].goalsAgainst, teams[x].totalPoints);
    	}
    	printf("|-----------------------------------------------------------------------------------\\|\n");
    	system("pause");
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Split from this 5 YEAR old thread
    Sorting an array alphabetically?

    See the rules.
    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
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > fflush(stdin);
    > gets(teams[x].name);
    Both of these are very wrong, see the FAQs
    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.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    OK, so it doesn't work - what's your question?

    Does it compile?
    Does it run or crash?
    Does it produce the expected output at all or only in some cases?

    What sort of input should we provide to test it?

    Don't just dump the code without any further text and expect people to fix it for you.
    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.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    5

    first part works

    THIS works fine what im trying to do with first code is save teams to a file

    Code:
    // Football.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    int menu();	// prototypes for functions to be called by main
    void enterteams();
    void enterresults();
    void printteams();
    
    
    
    typedef struct			// type definition for structure called person
    {
    	char name[20];
    	int played;
    	int won;
    	int drawn;
    	int lost;
    	int goalsFor;
    	int goalsAgainst;
    	int totalPoints;
    
    	
    } team;				
    
    team teams[12];		// array called teams, 12 elements of type team
    
    int main (void)			/* main calls menu() and this returns the option choice
    						   to the user.  Dependant on that choice, main() will call
    						   enterteams(), enterresults(),enterfixtures() or printteams(), or exit the program
    						   This will continue, once the three functions have finished
    						   their work until the user chooses option 4 - EXIT */
    {
    	int menuChoice;
    	do
    	{
    		menuChoice = menu();            // function is called and returns a value to menuChoice
    		if (menuChoice == 1)
    			enterteams();
    		if  (menuChoice == 2)
    		    enterresults();
    		else if (menuChoice == 3)
    		    printteams();
    	}while (menuChoice != 4);
    	system("pause");
    	return 0;
    }
    
    int menu()					           // offers choices to the user
    {
    	int choice;
    
    	system("cls");			           // clear screen
    
    	printf("\t\t\tMENU\n\t\t\t----\n\n");
    	printf("\t\t1.\tEnter Teams\n");
    	printf("\t\t2.\tEnter Results\n");
      	printf("\t\t3.\tDisplay current Table\n");
    	printf("\t\t4.\tExit\n\n");
    	printf("\t\tPlease enter choice: ");
    	scanf("%d", &choice);
    
    	return choice;
    }
    
    
    void enterteams()        //allows input of match fixtures
    {
    	
    	int x;
    	//char line[30];
    	system("cls");			// clear screen
    	for (x = 0; x < 4; x++)	
    	{
    		// scanf() only accepts one name-- so we use can use sscanf + gets()   
    		printf("Enter details for Teams %d\n\n",(x + 1)); 
    		printf("Name: ");
    		fflush(stdin);
    		gets(teams[x].name);		   
    		/*printf("Played: ");
    		scanf("%d", &teams[x].played);
    		printf("Won: ");
    		scanf("%d", &teams[x].won);
    		printf("Drawn: ");
    		scanf("%d", &teams[x].drawn);
    		printf("Lost: ");
    		scanf("%d", &teams[x].lost);
    		printf("Goals For: ");
    		scanf("%d", &teams[x].goalsFor);
    		printf("Goals Against: ");
    		scanf("%d", &teams[x].goalsAgainst);
    		printf("Total Points: ");
    		scanf("%d", &teams[x].totalPoints);
    		printf("Opposition: ");
    		scanf("%s", &teams[x].opposition);
    		printf("\n");*/
    
    	}
    }
    void enterresults()
    {
    	int x,y,hscore,ascore;
    	for (x = 0; x < 4; x++)
    		for (y =x+1; y <4; y++)
    		{
    			printf("Enter result for match between team %s and team %s: ", teams[x].name, teams[y].name);
    			scanf("%d %d", &hscore, &ascore);
    			teams[x].played++;
    			teams[y].played++;
    			teams[x].goalsFor = hscore+teams[x].goalsFor;
    			teams[y].goalsFor= ascore + teams[y].goalsFor;
    			teams[x].goalsAgainst = hscore+teams[x].goalsAgainst;
    			teams[y].goalsAgainst = ascore + teams[y].goalsAgainst;
    			if ( hscore == ascore)
    			{
    				teams[x].totalPoints++;
    				teams[y].totalPoints++;
    			}
    			if  (hscore>ascore)
    			{
    				teams[x].totalPoints+1;
    			}
    		    if  (hscore<ascore)
    			{
    				teams[y].totalPoints+1;
    			}
    		}
    }
    
    void printteams()
    {
    int x;
    system("cls");			// clear screen	
    	
    	printf("|--------------------------------------------------------------------------------\\|\n");
    	printf("| %-20s | %2s | %2s | %2s | %2s | %2s | %2s | %7s |\n", "Name", "P", "W", "D", "L", "F", "A", "Points" );
    	printf("|--------------------------------------------------------------------------------\\|\n");
    
    	for (x = 0; x < 4; x++)
    	{
    	printf("| %-20s | %2d | %2d | %2d | %2d | %2d | %2d | %7d |\n",teams[x].name, teams[x].played, teams[x].won, teams[x].drawn, teams[x].lost, teams[x].goalsFor, teams[x].goalsAgainst, teams[x].totalPoints);
    	}
    	printf("|-----------------------------------------------------------------------------------\\|\n");
    	system("pause");
    }

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > THIS works fine what im trying to do with first code is save teams to a file
    Well the simple fread / fwrite stuff seemed OK.
    I've no idea what the rest of the code which followed was supposed to do, it didn't look like it even compiled.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting: Getting permutation index array
    By flyvholm in forum C Programming
    Replies: 2
    Last Post: 09-20-2006, 07:07 PM
  2. Sorting an array
    By OnionKnight in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2005, 03:31 AM
  3. Array of Structures: Sorting
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 04-13-2005, 09:55 AM
  4. Replies: 2
    Last Post: 05-06-2003, 08:34 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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