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

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    31

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

    Hi i'm pretty new to C and am writing a program for a sports league table. I'm having a couple of problems:

    I have the following code in a function to add a new team to an array of structs called teams (teams[i].name where i is next free array space):

    Code:
    void addTeam(char *teamName, int i)
    {
    	strcpy(teams[i].name, teamName);
    }
    this code semi works. It works fine if the name i enter has no spaces (eg. ManUtd) However if i enter Man Utd it places Man in teams[i].name and it places Utd in teams[i+1].name for some reason. Any ideas why?

    Secondly I have made a function to try and sort an array of structs. I pass in the array of structs of type team, "teams". I call it as follows:

    sortTable(teams);

    Here is the code:

    Code:
    void sortTable(team teams)
    {
    	team temp[1];
    
    	for (int i = 0; i < 12; i++)
    	{
    		for (int j = 11; j >=0; j--)
    		{
    			if (teams[j].score > teams[(j-1)].score)
    			{ 	
    				temp[1] = teams[(j-1)];
    				teams[(j-1)] = teams[j];
    				teams[j] = temp[1];
    			}	
    		} 
    	}
    }
    Upon compiling i get the following errors:

    "In this statement, "teams" is of type "pointer to struct declared without a tag", and cannot be converted to "struct declared without a tag". noconvert "(on the line when i am calling sortTable outside the function)

    Secondly I get 6 errors saying "In this statement, "teams" has a struct type, but occurs in a context that requires a pointer. <needpointer>" (pointing to different lines of the code)

    A lot of questions I know but I'm pretty lost here. Any help greatly appreciated.

    Thanks

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Just a hunch, but are you reading the names in with something silly like scanf("%s", buffer);?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    31
    I am reading the name in by doing scanf("%s", newTeam); yes, as this is how I have been taught. Is this where the problem lies?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    scanf() stops reading a string at the first whitespace character. So if you enter "foo bar" then scanf() will first see "foo" and then when you call scanf() again it will see "bar". A much better function for reading in strings is fgets(). You could do something like:
    Code:
    fgets(newTeam, sizeof(newTeam), stdin);
    newTeam[strlen(newTeam)-1] = '\0';
    You have to do that second line of code because fgets() throws the \n into the string from when the user press ENTER.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    31
    Thanks, didn't realise scanf stopped reading at a space. However I placed that code after my printf statement and it read nothing in at all.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    After what printf() statement?
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    9
    And just for future reference, scanf() can be very picky when taking data.

    if you throw it something other than what it is expecting, u will usually cause some really wierd results. A good thing to get to know is fgets and all the get functions in C, they will help you a lot more than scanf, at least they did in my experiences.

    Just my 2 cents...

    The Landroid

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    31
    Quote Originally Posted by itsme86
    After what printf() statement?
    Thanks for the help so far guys.

    The printf statement i mean is that i do a printf statement saying "add new team name." i placed the code you wrote after this, replacing my scanf statement, as that is how i understood it

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You probably have a scanf() call in front of it then. scanf() leaves the \n on the input butter so fgets() interprets it as the user just pressing ENTER without entering anything. Another fgets() call would grab what the user actually typed. There's a whole FAQ on this site about getting input from the user. You might want to read it...
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    31
    Thanks, problem sorted. I'll check out that FAQ.

    Does anyone know anything about the pointer problems?

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    void sortTable(team teams)
    I don't know what that is. Are you using a C++ compiler or have you used a typedef? Seeing your whole code would really have prevented me from having to have asked all the questions I've had to ask in this thread.
    If you understand what you're doing, you're not learning anything.

  12. #12
    Registered User
    Join Date
    Mar 2005
    Posts
    31
    sorry i'll paste the whole code

    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];
    			}	
    		} 
    	}
    }

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Renaming teams to teams1 in sortTable didn't get rid of the errors?
    If you understand what you're doing, you're not learning anything.

  14. #14
    Registered User
    Join Date
    Mar 2005
    Posts
    31
    no it didn't

  15. #15
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    void sortTable(team teams1)
    This is saying that teams1 is a single instance of the struct, but in the function you're trying to use it as an array. Try this instead:
    Code:
    void sortTable(team teams1[])
    EDIT: Don't forget to change the function's prototype also.
    Last edited by itsme86; 03-03-2005 at 06:31 PM.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed