Thread: Need Help with structures.

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    New Zealand
    Posts
    13

    Need Help with structures.

    Hi,
    I'm making a program to play a pretty simplified blackjack game. I need to use abstract data types and am having a bit of trouble.

    At the moment i am just trying to create a deck of cards.
    I have a structure for the deck of cards and a structure for each card. (as shown)
    I'm trying to create a function to fill my deck structure with card structures so that the deck is filled with 52 standard cards.
    This is what i have so far:

    This is my card structure
    Code:
    enum card_suit { HEARTS, DIAMONDS, CLUBS, SPADES };
    enum card_value { ACE = 1, JACK = 11, QUEEN = 12, KING = 13 };
    
    typedef struct
    {
       enum card_value value;
       enum card_suit suit;
       int face_up;
    } card_t;
    And this is my deck structure:
    Code:
    #define MAX_CARDS_IN_DECK 52
    
    typedef struct
    {
       card_t deck[MAX_CARDS_IN_DECK];
    } deck_t;
    And this is my function to try fill the deck:
    Code:
    deck_t create_deck(deck_t deck)
    {
       int i,j;
       deck_t * deck_ptr;
       deck_ptr = deck;         //I think this is the problem but im not sure how to fix it
    
       
       for (i = 0; i < 4; i++)
       {
          for (j = 1; j < 14; j++)
          {
                    deckptr -> value = j;
    		deckptr -> suit = i;
       		deckptr++;
          }
       }
    }
    Please help.
    Chris.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    deck_t * deck_ptr = &deck;

    what were the errors?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    New Zealand
    Posts
    13
    That solved that error, have new ones now but i should be able to sort them out.
    Thank you.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Location
    New Zealand
    Posts
    13
    Ok, maybe i wont be able to sort them out.

    I now get an error messages saying:
    error: 'struct <anonymous>' has no member named 'value'
    error: 'struct <anonymous>' has no member named 'suit'

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are passing, by value, one deck_t, pointing to that, and then incrementing off into oblivion. While not the help you were seeking, it's one problem you've got.


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

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    i and j are integers, value and suit are enums. They don't play well together. You might as well make all your card data in integer form.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually the real problem is that they're pointing to and passing, a deck_t, but treating it as if it were a card_t. deck_t has no value or suit.

    You know, just like the error says...

    Had they actually tagged their structures, they might have noticed this.


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

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    Why not just define it as: deck_t create_deck(deck_t *deck_ptr) ?

    Also, syntax errors here:
    Code:
          for (j = 1; j < 14; j++)
          {
                    deckptr -> value = j;
    		deckptr -> suit = i;
       		deckptr++;
          }
    Your variable is called deck_ptr, not deckptr
    "There are three kinds of people in the world...
    Those that can count and those that can't."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures within Structures
    By Misko82 in forum C Programming
    Replies: 2
    Last Post: 08-27-2007, 12:25 AM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM