couple problems with structures

This is a discussion on couple problems with structures within the C Programming forums, part of the General Programming Boards category; Here's the assignment: "You must create a structure called Player, that contains each of the following variables: - A string ...

  1. #1
    DLR
    DLR is offline
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34

    couple problems with structures

    Here's the assignment:
    "You must create a structure called Player, that contains each of the following variables:
    - A string called name that has a maximum size of 10 characters.
    - A character called major that corresponds to a particular major within engineering. The key is as follows:
    e = ECE
    m = MIE
    c = CEE
    h = ChE
    b = BME
    u = Und
    - And an integer called seed that indicates the pre-tournament ranking of each player (from 1 – 8).

    Your program must create an array of 8 Player structures. Then, you must create a function to allow the user to input each of the players’ name and major. You will need to provide error checking code for the major variable. The major variable will need to be checked to make sure it is one of the acceptable characters listed above.

    Have the user input the players’ information in seed order. That is, enter the number one seed’s information first, then the number two seed, etc. Your array should be filled in this order as well (i.e. number one seed in array index 0, number two seed in array index 1...etc.).

    Finally, you will call the printBracket function to display the bracket as it currently stands. "

    I know that what I have so far has some big errors, but don't knwo how to fix any of them. What am I doing wrong?

    Code:
    #include <stdio.h>
    
    
    /**************************************FUNCTION PROTOTYPES**/
    
    void printBracket( struct Player x []);
    int getIndex( char c );
    void swoopinfo(char *, char *);
    
    
    
    struct player {
    	int seed;
    	char name[11];
    	char major;
    	
    };
    
    typedef struct player Player;
    
    int main()
    {
    	int i=1;
    	char firstname;
    	char theirmajor;
    
    	printf("Please enter the 8 player names, followed by their major code and seed. \n For major enter a single character from the major code key:\n'e' for ECE, 'h' for ChE, 'm' for MIE\n'c' for CEE, 'b' for BME, 'u' for Und \n");
    
    
    		for (i=1;i<9;i++)
    		{
    
    	void swoopinfo(&firstname,&theirmajor);
    	Player i= {i,firstname,theirmajor};
    		}
    	
    
    }
    
    
    
    	
    
    
    /**************************************FUNCTIONS**/
    
    
    	void swoopinfo(char *nameptr, char *majorptr)
    			{
    				printf("enter name:  ");
    				scanf("%s", *nameptr);
    
    				printf("enter %s's major:  ", *nameptr);
    				scanf("%c", *majorptr);
    		}
    
    
    
    
    
    //*******************************FUNCTION: printBracket
    //*******************************ARGUMENTS: ( struct Player [] ): Array of team structures sorted by seed
    //*******************************RETURN: void
    //*******************************DESCRIPTION: prints out bracket image of sorted array of players
    void printBracket( struct Player x [] )
    {
    	char * maj[6]= {"ECE", "ChE", "MIE", "CEE", "BME", "Und"};  //array used for indexing major
    
    	//PRINTING THE BRACKET
    	printf("\n\n");
    	printf("__%-10s_(%s)_(%d)_\n", x[0].name, maj[getIndex(x[0].major)], x[0].seed );
    	printf("                       |_________\n");
    	printf("__%-10s_(%s)_(%d)_|         |\n", x[7].name, maj[getIndex(x[7].major)], x[7].seed );
    	printf("                                 |_________\n");
    	printf("__%-10s_(%s)_(%d)_          |         |\n", x[3].name, maj[getIndex(x[3].major)], x[3].seed );
    	printf("                       |_________|         |\n");
    	printf("__%-10s_(%s)_(%d)_|                   |\n", x[4].name, maj[getIndex(x[4].major)], x[4].seed );
    	printf("                                           |_________\n");
    	printf("__%-10s_(%s)_(%d)_                    |\n", x[1].name, maj[getIndex(x[1].major)], x[1].seed );
    	printf("                       |_________          |\n");
    	printf("__%-10s_(%s)_(%d)_|         |         |\n", x[6].name, maj[getIndex(x[6].major)], x[6].seed );
    	printf("                                 |_________|\n");
    	printf("__%-10s_(%s)_(%d)_          |\n", x[2].name, maj[getIndex(x[2].major)], x[2].seed );
    	printf("                       |_________|\n");
    	printf("__%-10s_(%s)_(%d)_|\n", x[5].name, maj[getIndex(x[5].major)], x[5].seed );
    
    }
    /////////////////END OF printBracket FUNCTION\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    
    
    //*******************************FUNCTION: getIndex
    //*******************************ARGUMENTS: ( char ): character to index
    //*******************************RETURN: int
    //*******************************DESCRIPTION: returns the appropriate index of the major character in the string array of printBracket
    int getIndex( char c )
    {
    	switch( c )
    	{
    		case 'e':
    		case 'E':
    			return 0;
    			break;
    		case 'h':
    		case 'H':
    			return 1;
    			break;
    		case 'm':
    		case 'M':
    			return 2;
    			break;
    		case 'c':
    		case 'C':
    			return 3;
    			break;
    		case 'b':
    		case 'B':
    			return 4;
    			break;
    		case 'u':
    		case 'U':
    			return 5;
    			break;
    		default:
    			return 5;
    	}
    }
    /////////////////END OF getIndex FUNCTION\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Ok i didn't fix all of your code but here are the first things you
    need to do:

    Code:
    void printBracket( struct Player x []);
    int getIndex( char c );
    void swoopinfo(char *, char *);
    
    
    
    struct player {
    	int seed;
    	char name[11];
    	char major;
    	
    };
    
    typedef struct player Player;
    should be switched around to this:

    Code:
    struct player {
    	int seed;
    	char name[11];
    	char major;
    	
    };
    
    typedef struct player Player;
    
    void printBracket(Player x []); 
    
    /*Note that the struct keyword was removed here
    - need to remove it from function definition as well*/
    
    int getIndex( char c );
    void swoopinfo(char *, char *);
    now there's this:

    Code:
    Player i= {i,firstname,theirmajor};
    That is very wrong because from what i can gather, you want to
    set the attributes of a Player struct to these values - thats not
    how to do it- look up structs.

    what you will need is an array of structs - like this:

    Code:
    Player list [9];
    also there's this:

    Code:
    char firstname;
    that only handles a single character - you need

    Code:
    char firstname [11]; /*The size is the same as the string in struct itself*/
    here's how you would then put the data into each element of the
    struct array:

    Code:
    for (i=1;i<9;i++)
    {
           swoopinfo(firstname,&theirmajor);
           list [i].seed = i;
           strcpy (list [i].name, firstname);
           list [i].major = theirmajor;
    }
    notice the use of strcpy - you'll need string.h

    Lasty, swoopinfo will have to be changed to this:

    Code:
    void swoopinfo(char nameptr [], char *majorptr)
    {
           printf("enter name:  ");
           scanf("%s", nameptr);
    
           /*Probably need a buffer flush here*/
    
           printf("enter %s's major:  ", nameptr);
           scanf("%c", majorptr);
    }
    As i said, i didn't fix all your code, but make the changes and it
    should compile and get you in the right direction. I didn't verify
    if it works completely, but it starts reading in values so i leave
    any further problems for you to fix - hence the mention of a buffer
    flush.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    DLR
    DLR is offline
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    cool thanks

    ...are you sure that you're not supposed to have
    Code:
    void struct printBracket(Player x []);
    ?

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    well for your last post just now, that makes no sense:

    void struct printBracket(Player x []);

    that looks like its returning void and a struct, thats the closest
    thing that it looks like (for anyone else reading, i know that struct
    on its own would not return a stuct, i know that you would need
    the struct's name, i'm just attempting to rationalise the above
    irrational syntax).

    what you are referring to (i think) is this:

    void printBracket( struct Player x []);

    the reason i removed the struct keyword is because of this:

    typedef struct player Player;

    as you are aware, C is case sensitive - but the reason you use a
    typedef with a struct is so that you don't have to type struct every
    time you want to mention a specific struct type. In short, the
    compiler will recognise "struct player;" as a legal data type,
    but not "struct Player", but it will recognise "Player" as being
    equivalent to "struct player" due to the typedef. hope that clears
    it up.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    DLR
    DLR is offline
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    Quote Originally Posted by Richie T
    well for your last post just now, that makes no sense:

    void struct printBracket(Player x []);

    that looks like its returning void and a struct, thats the closest
    thing that it looks like (for anyone else reading, i know that struct
    on its own would not return a stuct, i know that you would need
    the struct's name, i'm just attempting to rationalise the above
    irrational syntax).

    what you are referring to (i think) is this:

    void printBracket( struct Player x []);

    the reason i removed the struct keyword is because of this:

    typedef struct player Player;

    as you are aware, C is case sensitive - but the reason you use a
    typedef with a struct is so that you don't have to type struct every
    time you want to mention a specific struct type. In short, the
    compiler will recognise "struct player;" as a legal data type,
    but not "struct Player", but it will recognise "Player" as being
    equivalent to "struct player" due to the typedef. hope that clears
    it up.
    oh yeah i guess that makes sense....I'm just going to get ride of the typedef

  6. #6
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    or you could just change the occurance of struct Player in the
    function to struct player - that would keep the code i gave you
    in tact - you'd get a warning about type conversion though if you
    pass something of type "Player" to a function expecting "struct
    player", but thats ok. Its up to you, i prefer the typedef way all
    the way through a program so i'd change any instances of "struct
    player" to Player, but thats just me
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  7. #7
    DLR
    DLR is offline
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    haha well I finally figured out why i kept getting 56 errors.....the printBracket calls for Player with a capital P, I had the Player structure as player.

  8. #8
    DLR
    DLR is offline
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    Here's what I have. It should work from what I can tell, but it has a little bit of a problem printing the bracket. Also there's a few warning messages, which I am pretty sure are because I'm sending i down to the swoopinfo function
    Code:
    swoopinfo(firstname,i,&theirmajor);
    .


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    /* structure for player info*/
    struct Player {	
    	int seed;
    	char name[11];
    	char major;
    	
    };
    typedef struct Player Player;   //won't have to type in "struct" all 1 times in the main...sure was worth it
    
    
    
    void printBracket(Player x []);   //prints the brackets
    
    
    int getIndex( char c );				//used in printBracket
    void swoopinfo(char *, char *);		//function to get player info from user
    
    int main()
    {	
    	Player list[9];
    	int i=1;
    	char firstname[11];
    	char theirmajor;
    
    	printf("Please enter the 8 player names,\n followed by their major code and seed.\n"
    		 "For major enter a single character from the major code key:\n\n"
    		 "'e' for ECE 'h' for ChE \n'm' for MIE 'c' for CEE\n 'b' for BME 'u' for Und \n\n\n");
    
    		/* run swoopinfo function, set player info for each player, move to next seed*/
    		for (i=1;i<9;i++)
    {
           swoopinfo(firstname,i,&theirmajor);
           list [i].seed = i;
    
           strcpy (list [i].name, firstname);
           list [i].major = theirmajor;
    }
    
    	printBracket(list);  //print the bracket
    	
    	return(0);
    
    
    		
    	
    
    }
    
    
    
    	
    
    
    /**************************************FUNCTIONS**/
    
    
    void swoopinfo(char nameptr [],int i, char *majorptr)
    {		
    	    char dummy;
           printf("Enter seed #%d's name:  ",i);  
           scanf("%s", nameptr);   //used in main
    
           printf("Enter %s's major:  ", nameptr);
           scanf("%c", &dummy);    //need a buffer apparently
    	   scanf("%c", &majorptr);    //used in main
    }
    
    
    
    //*******************************FUNCTION: printBracket
    //*******************************ARGUMENTS: ( struct Player [] ): Array of team structures sorted by seed
    //*******************************RETURN: void
    //*******************************DESCRIPTION: prints out bracket image of sorted array of players
    void printBracket( struct Player x [] )
    {
    	char * maj[6]= {"ECE", "ChE", "MIE", "CEE", "BME", "Und"};  //array used for indexing major
    
    	//PRINTING THE BRACKET
    	printf("\n\n");
    	printf("__%-10s_(%s)_(%d)_\n", x[0].name, maj[getIndex(x[0].major)], x[0].seed );
    	printf("                       |_________\n");
    	printf("__%-10s_(%s)_(%d)_|         |\n", x[7].name, maj[getIndex(x[7].major)], x[7].seed );
    	printf("                                 |_________\n");
    	printf("__%-10s_(%s)_(%d)_          |         |\n", x[3].name, maj[getIndex(x[3].major)], x[3].seed );
    	printf("                       |_________|         |\n");
    	printf("__%-10s_(%s)_(%d)_|                   |\n", x[4].name, maj[getIndex(x[4].major)], x[4].seed );
    	printf("                                           |_________\n");
    	printf("__%-10s_(%s)_(%d)_                    |\n", x[1].name, maj[getIndex(x[1].major)], x[1].seed );
    	printf("                       |_________          |\n");
    	printf("__%-10s_(%s)_(%d)_|         |         |\n", x[6].name, maj[getIndex(x[6].major)], x[6].seed );
    	printf("                                 |_________|\n");
    	printf("__%-10s_(%s)_(%d)_          |\n", x[2].name, maj[getIndex(x[2].major)], x[2].seed );
    	printf("                       |_________|\n");
    	printf("__%-10s_(%s)_(%d)_|\n", x[5].name, maj[getIndex(x[5].major)], x[5].seed );
    
    }
    /////////////////END OF printBracket FUNCTION\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    
    
    //*******************************FUNCTION: getIndex
    //*******************************ARGUMENTS: ( char ): character to index
    //*******************************RETURN: int
    //*******************************DESCRIPTION: returns the appropriate index of the major character in the string array of printBracket
    int getIndex( char c )
    {
    	switch( c )
    	{
    		case 'e':
    		case 'E':
    			return 0;
    			break;
    		case 'h':
    		case 'H':
    			return 1;
    			break;
    		case 'm':
    		case 'M':
    			return 2;
    			break;
    		case 'c':
    		case 'C':
    			return 3;
    			break;
    		case 'b':
    		case 'B':
    			return 4;
    			break;
    		case 'u':
    		case 'U':
    			return 5;
    			break;
    		default:
    			return 5;
    	}
    }
    /////////////////END OF getIndex FUNCTION\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

  9. #9
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    found the problem, my fault actually:

    Code:
    for (i=1;i<9;i++)
    {
           swoopinfo(firstname,i,&theirmajor);
           list [i].seed = i;
    
           strcpy (list [i].name, firstname);
           list [i].major = theirmajor;
    }
    array indices start at 0, this is starting at 1 - change code to
    this:

    Code:
    for (i=1;i<9;i++)
    {
           swoopinfo(firstname,i,&theirmajor);
           list [i-1].seed = i;
    
           strcpy (list [i].name, firstname);
           list [i-1].major = theirmajor;
    }
    that'll preserve the numbering scheme printed on screen

    also fix this:

    void swoopinfo(char *, char *);

    should be:

    void swoopinfo(char *, int, char *);

    produces an error for me otherwise, surprising that it compiled
    on your setup.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  10. #10
    DLR
    DLR is offline
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    Quote Originally Posted by Richie T
    also fix this:

    void swoopinfo(char *, char *);

    should be:

    void swoopinfo(char *, int, char *);

    produces an error for me otherwise, surprising that it compiled
    on your setup.
    oh duh...yeah i'm surprised it worked too. thanks a lot!

  11. #11
    DLR
    DLR is offline
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    it's still not working right. It always prints the persons major as Und. I've tried everything I can think of. Right now it works best with
    Code:
    for (i=0;i<8;i++)
    {
           swoopinfo(firstname,i,&theirmajor);
           list [i].seed = i;
    
           strcpy (list [i].name, firstname);
           list [i].major = theirmajor;
    and in the swoopinfo function:
    Code:
    printf("Enter seed #%d's name:  ",(i+1));

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So how about you add some simple printf statements to your code to help you debug. Print out useful things like, oh, I don't know, the value entered for major... Scatter these types of things throughout your program and you can see what value is entered / stored where, whenever you like.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    DLR
    DLR is offline
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    ok I'll try that. I apologize for not being a good programmer like yourself.

  14. #14
    DLR
    DLR is offline
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    from what I can tell the function is getting the char for major correctly...so I think that it's not sending it back to main correctly.

  15. #15
    DLR
    DLR is offline
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    well if anybody gives a damn i figured it out:

    Code:
    void swoopinfo(char nameptr [], int i, char *majorptr)
    {		
    		char plezwork;   //it worked!
    	    char dummy;
           printf("Enter seed #%d's name:  ",(i));  
           scanf("%s", nameptr);   //used in main
    
           printf("Enter %s's major:  ", nameptr);
           scanf("%c", &dummy);  //need a buffer apparently
    	   scanf("%c", &plezwork); //find out what their major is
    	   *majorptr = plezwork;  //set majorptr to whatever plezwork is and send it up to main
    	   printf("\n");
    }

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

Similar Threads

  1. openGL problems (glut)
    By c_young in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2007, 01:27 PM
  2. array of structures help!
    By voodoo3182 in forum C Programming
    Replies: 12
    Last Post: 08-03-2005, 03:58 PM
  3. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM

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