[ noob question ] Help with structs within structs

This is a discussion on [ noob question ] Help with structs within structs within the C Programming forums, part of the General Programming Boards category; I have resolved this issue by creating an array of strings. Thank you to all for who assisted me. ------------------------------------------------------------- ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    [ noob question ] Help with structs within structs

    I have resolved this issue by creating an array of strings.

    Thank you to all for who assisted me.

    -------------------------------------------------------------


    Hi, I am working on an assignment in which I am supposed to code 5-Card Draw poker.
    I am trying to keep track of which cards have been dealt and am doing so using structs.

    Here are my structs:

    Code:
    typedef struct cardtype
    {
    	char face[20];
    	char suit[20];
    
    }Cardtype;
    
    typedef struct choices
    {
    	Cardtype card1;
    	Cardtype card2;
    	Cardtype card3;
    	Cardtype card4;
    	Cardtype card5;
    } Choices;

    Later on in my program, when trying to actually keep track of these, I am doing this, but when I attempt to print out the cards that are being kept track of, it shows nothing, and upon debugging all the data in the struct is 0.

    Help!

    Code:
    	/* if slot contains current card, display card */
    				if (wDeck[row][column] == card)
    				{
    					printf ("%5s of %-8s%c", wFace[column], wSuit[row], card % 2 == 0 ? '\n' : '\t');
    					
    					switch ( i )
    					{
    					case 1:
    						strcpy( player1.card1.face, wFace[column]);
    						strcpy( player1.card1.suit, wSuit[row]);
    						break;
    					case 2:
    						strcpy( player1.card2.face, wFace[column]);
    						strcpy( player1.card2.suit, wSuit[row]);
    						break;
    					case 3:
    						strcpy( player1.card3.face, wFace[column]);
    						strcpy( player1.card3.suit, wSuit[row]);
    						break;
    					case 4:
    						strcpy( player1.card4.face, wFace[column]);
    						strcpy( player1.card4.suit, wSuit[row]);
    						break;
    					case 5:
    						strcpy( player1.card5.face, wFace[column]);
    						strcpy( player1.card5.suit, wSuit[row]);
    						break;
    					}
    
    					i++;
    					
    					
    					
    				}
    Last edited by Riverfoot; 04-26-2011 at 09:15 PM. Reason: Resolved issue

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,577
    I think you are using poorly designed structs. But, since the code you posted has such a small part of your stated problem I have no idea where you made any other mistake.

    Hint: Most beginners use ints to represent card faces and suits instead of C-strings.

    Tim S.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Riverfoot View Post
    Hi, I am working on an assignment in which I am supposed to code 5-Card Draw poker.
    I am trying to keep track of which cards have been dealt and am doing so using structs.
    Ok... that's not the way to do it.

    Your best bet is to build an array of 52 integers... then assign each a value from 0 to 51... and then shuffle the deck.

    To represent a card you take the number from the array slot and do just a little math... first to decide which suit you use modulous math to get a result between 0 and 3 .. 0 = Spades, 1 = Clubs, etc. Next you divide by 4 to get the card value... 0 to 13 ... 0 = Ace, etc.

    Since you deal from the top of the deck, you keep an index value that tells you which array slot is next and is decremented each time a player takes a card.

    Really... work it out on paper... it's a whole lot simpler than how you're doing it.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    2
    Ok, thank you guys for the help, but I suppose I should provide more back story since I didn't know I was having that big of an issue.

    We have to work off of some given code, and I guess ill just paste it in here:
    Main:

    Code:
    int main (void)
    {
    
    	Choices *player1 = { '\0'} ;
    	//Choices *player2 = { {'\0','\0'} , {'\0','\0'} , {'\0','\0'} , {'\0','\0'} , {'\0','\0'} } ;
    	/* initialize suit array */
    	const char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};
    
    	/* initialize face array */
    	const char *face[13] = {"Ace", "Deuce", "Three", "Four", "Five", "Size", "Seven", "Eight",
    		"Nine", "Ten", "Jack", "Queen", "King"};
    
    	/* initalize deck array */
    	int deck[4][13] = {0};
    
    	srand ((unsigned) time (NULL)); /* see random-number generator */
    
    	shuffle (deck);
    	deal (deck, face, suit, player1);
    
    	printf("\n\n%s %s", player1->card2.face, player1->card2.suit);
    	
    
    	return 0;
    }

    and heres my dealing function

    Code:
    void deal (const int wDeck[][13], const char *wFace[], const char *wSuit[], Choices *player1)
    {
    	int row = 0;    /* row number */
    	int column = 0; /*column number */
    	int card = 0;   /* card counter */
    	int i = 1;
    
    	printf("You were dealt:\n\n");
     
    	/* deal each of the 52 cards */
    	for (card = 1; card <= 5; card++)
    	{
    		/* loop through rows of wDeck */
    		for (row = 0; row <= 3; row++)
    		{
    			/* loop through columns of wDeck for current row */
    			for (column = 0; column <= 12; column++)
    			{
    				/* if slot contains current card, display card */
    				if (wDeck[row][column] == card)
    				{
    					printf ("%5s of %-8s%c", wFace[column], wSuit[row], card % 2 == 0 ? '\n' : '\t');
    					
    					switch ( i )
    					{
    					case 1:
    						strcpy( player1->card1.face, wFace[column]);
    						strcpy( player1->card1.suit, wSuit[row]);
    						break;
    					case 2:
    						strcpy( player1->card2.face, wFace[column]);
    						strcpy( player1->card2.suit, wSuit[row]);
    						break;
    					case 3:
    						strcpy( player1->card3.face, wFace[column]);
    						strcpy( player1->card3.suit, wSuit[row]);
    						break;
    					case 4:
    						strcpy( player1->card4.face, wFace[column]);
    						strcpy( player1->card4.suit, wSuit[row]);
    						break;
    					case 5:
    						strcpy( player1->card5.face, wFace[column]);
    						strcpy( player1->card5.suit, wSuit[row]);
    						break;
    					}
    
    					i++;
    					
    					
    					
    				}
    			}
    		}
    	}
    }

    Thanks again for the help, this is really awesome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Structs Into An Array Of Structs.
    By TheTaoOfBill in forum C Programming
    Replies: 3
    Last Post: 10-07-2010, 09:38 AM
  2. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  3. Structs question
    By BigFish21 in forum C Programming
    Replies: 25
    Last Post: 04-23-2008, 09:57 PM
  4. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 06:14 PM
  5. Question about structs
    By Imperito in forum C Programming
    Replies: 6
    Last Post: 08-30-2002, 03:18 PM

Tags for this Thread


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