A couple of beginner problems, one regarding strcpy and another regarding pointers

This is a discussion on A couple of beginner problems, one regarding strcpy and another regarding pointers within the C Programming forums, part of the General Programming Boards category; Thank you that was it much appreciated...

  1. #16
    Registered User
    Join Date
    Mar 2005
    Posts
    31
    Thank you that was it

    much appreciated

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    by looking at you code, i found some simples synatx error, which probabaly doesn't make the prog to compile rather it through you some couple of errors. look at the code which i have highlighted on you code. and below that is the working version of program. check out the changes what i have made on your code. those are some of the error which you have made in your code. and i have explanied what it is below: -

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void addTeam(char[50], int);
    void calculateResult(int, int, int, int);
    
    char newTeam[50];
    int menuChoice = 0;
    int numOfTeams = 0;
    int team1, team2, team1Score, team2Score;
    
    typedef struct
    {
    	char name[50];
    	int points, goalsFor, goalsAgainst, played, won, lost, drawn;
    } team;
    
    team teams[12];
    
    void sortTable(team);
      
    int main(void)
    {
    	while (menuChoice != 4)
    	{
    		printf("Football League\n\n");
    		printf("1. Add team\n");
    		printf("2. Display league table\n");
    		printf("3. Add result\n");
    		printf("4. Quit\n");
    		scanf("%d", &menuChoice);	
    
    		if (menuChoice == 1)
    		{
    			if (numOfTeams == 11)
    				printf("Error: Maximum amount of teams has been entered");
    			else
    			{
    				printf("Add new team\n");
    				fgets(newTeam, sizeof(newTeam), stdin);
    				newTeam[strlen(newTeam)-1] = '\0';
    				fgets(newTeam, sizeof(newTeam), stdin);
    				newTeam[strlen(newTeam)-1] = '\0';
    				addTeam(newTeam, numOfTeams);
    				numOfTeams++;
    				printf("\nNew team added\n");
    			}
    		}		
    		else if (menuChoice == 2)
    		{
    			printf("\t\tP\tW\tD\tL\tF\tA\tT\n\n\n");
    			for (int i = 0; i < 12; i++)
    			{
    				printf("%s\t\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", 
    				teams[i].name, teams[i].played, teams[i].won,teams[i].drawn, 
    				teams[i].lost, teams[i].goalsFor, teams[i].goalsAgainst, 
    teams[i].points);
    			}
    		}
    		else if (menuChoice == 3)
    		{
    			printf("Enter first team playing:\n");
    			scanf("%d", &team1);
    			printf("Enter second team playing:\n");
    			scanf("%d", &team2);
    			printf("Enter first teams score:\n");
    			scanf("%d", &team1Score);
    			printf("Enter second teams score:\n");
    			scanf("%d", &team2Score);
    			calculateResult(team1, team2, team1Score, team2Score);
    		}
    
    	}
    return 0;
    }
     
    void addTeam(char *teamName, int i)
    {
    	strcpy(teams[i].name, teamName);
    	teams[i].points = 0;
    	teams[i].goalsFor = 0;
    	teams[i].goalsAgainst = 0;
    	teams[i].played = 0;
    	teams[i].won = 0;
    	teams[i].lost = 0;
    	teams[i].drawn = 0;
    }
    
    void calculateResult(int t1, int t2, int t1R, int t2R)
    {
    	if (t1R > t2R)
    	{
    		teams[(t1-1)].points+=3;
    		teams[(t1-1)].won++;
    		teams[(t2-1)].lost++;	
    	}
    	else if (t2R > t1R)
    	{
    		teams[(t2-1)].points+=3;
    		teams[(t2-1)].won++;
    		teams[(t1-1)].lost++;
    	}
    	else
    	{
    		teams[(t2-1)].points++;
    		teams[(t1-1)].points++;
    		teams[(t1-1)].drawn++;
    		teams[(t2-1)].drawn++;
    	}
    	teams[(t1-1)].goalsFor+=t1R;
    	teams[(t2-1)].goalsFor+=t2R;
    	teams[(t1-1)].goalsAgainst+=t2R;
    	teams[(t2-1)].goalsAgainst+=t1R;
    	teams[(t1-1)].played++;
    	teams[(t2-1)].played++;
    
    	sortTable(teams);
    }
    
    void sortTable(team teams1)
    {
    	team temp[1];
    
    	for (int i = 0; i < 12; i++)
    	{
    		for (int j = 11; j >=0; j--)
    		{
    			if (teams1[j].score > teams1[(j-1)].score)
    			{ 	
    				temp[1] = teams1[(j-1)];
    				teams1[(j-1)] = teams1[j];
    				teams1[j] = temp[1];
    			}	
    		} 
    	}
    }
    1. u are not allowed to declare the variable in between the program. all the variables have to declared after the main.
    2. chect out the sorttable function prototype. it takes a array of struct team which should be declared somehting like this void sortTable(team []);
    3. and in the sorttable defination you checking the two vlaue of scores where score is not the data memeber of struct team. i think it is points as per your struct defination

    so these are some of the errors which i identified. hope this will help u out. and below is the working prog od yours.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void addTeam(char[50], int);
    void calculateResult(int, int, int, int);
    
    char newTeam[50];
    int menuChoice = 0;
    int numOfTeams = 0;
    int team1, team2, team1Score, team2Score;
    
    typedef struct
    {
    	char name[50];
    	int points, goalsFor, goalsAgainst, played, won, lost, drawn;
    } team;
    
    team teams[12];
    
    void sortTable(team[]);
      
    int main(void)
    {
    	int i;
        while (menuChoice != 4)
    	{
    		printf("Football League\n\n");
    		printf("1. Add team\n");
    		printf("2. Display league table\n");
    		printf("3. Add result\n");
    		printf("4. Quit\n");
    		scanf("%d", &menuChoice);	
    
    		if (menuChoice == 1)
    		{
    			if (numOfTeams == 11)
    				printf("Error: Maximum amount of teams has been entered");
    			else
    			{
    				printf("Add new team\n");
    				fgets(newTeam, sizeof(newTeam), stdin);
    				newTeam[strlen(newTeam)-1] = '\0';
    				fgets(newTeam, sizeof(newTeam), stdin);
    				newTeam[strlen(newTeam)-1] = '\0';
    				addTeam(newTeam, numOfTeams);
    				numOfTeams++;
    				printf("\nNew team added\n");
    			}
    		}		
    		else if (menuChoice == 2)
    		{
    			printf("\t\tP\tW\tD\tL\tF\tA\tT\n\n\n");
    			for ( i = 0; i < 12; i++)
    			{
    				printf("%s\t\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", 
    				teams[i].name, teams[i].played, teams[i].won,teams[i].drawn, 
    				teams[i].lost, teams[i].goalsFor, teams[i].goalsAgainst, 
    teams[i].points);
    			}
    		}
    		else if (menuChoice == 3)
    		{
    			printf("Enter first team playing:\n");
    			scanf("%d", &team1);
    			printf("Enter second team playing:\n");
    			scanf("%d", &team2);
    			printf("Enter first teams score:\n");
    			scanf("%d", &team1Score);
    			printf("Enter second teams score:\n");
    			scanf("%d", &team2Score);
    			calculateResult(team1, team2, team1Score, team2Score);
    		}
    
    	}
    return 0;
    }
     
    void addTeam(char *teamName, int i)
    {
    	strcpy(teams[i].name, teamName);
    	teams[i].points = 0;
    	teams[i].goalsFor = 0;
    	teams[i].goalsAgainst = 0;
    	teams[i].played = 0;
    	teams[i].won = 0;
    	teams[i].lost = 0;
    	teams[i].drawn = 0;
    }
    
    void calculateResult(int t1, int t2, int t1R, int t2R)
    {
    	if (t1R > t2R)
    	{
    		teams[(t1-1)].points+=3;
    		teams[(t1-1)].won++;
    		teams[(t2-1)].lost++;	
    	}
    	else if (t2R > t1R)
    	{
    		teams[(t2-1)].points+=3;
    		teams[(t2-1)].won++;
    		teams[(t1-1)].lost++;
    	}
    	else
    	{
    		teams[(t2-1)].points++;
    		teams[(t1-1)].points++;
    		teams[(t1-1)].drawn++;
    		teams[(t2-1)].drawn++;
    	}
    	teams[(t1-1)].goalsFor+=t1R;
    	teams[(t2-1)].goalsFor+=t2R;
    	teams[(t1-1)].goalsAgainst+=t2R;
    	teams[(t2-1)].goalsAgainst+=t1R;
    	teams[(t1-1)].played++;
    	teams[(t2-1)].played++;
    
    	sortTable(teams);
    }
    
    void sortTable(team teams1[])
    {
    	team temp[1];
    	int i, j;
    	for ( i = 0; i < 12; i++)
    	{
    		for ( j = 11; j >=0; j--)
    		{
    			if (teams1[j].points > teams1[(j-1)].points)
    			{ 	
    				temp[1] = teams1[(j-1)];
    				teams1[(j-1)] = teams1[j];
    				teams1[j] = temp[1];
    			}	
    		} 
    	}
    }
    s.s.harish

  3. #18
    Registered User
    Join Date
    Mar 2005
    Posts
    31
    cheers harish, changes made.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

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