Thread: Problem with conversion from stuct

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    3

    Problem with conversion from stuct

    Thank you for looking at my thread. Below is the code I have:
    The bolded function is where the compiler says the error is

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    
    
    typedef enum { clubs, diamonds, hearts, spades} suit_t;
    typedef struct
    {
    unsigned int number : 4;
    suit_t suit : 2;
    const char * Face[];
    char Facevalue;
    int i;
    suit_t suitValue;
    }card_t;
    
    
    // function prototypes
    
    
    void fillDeck( card_t *);
    
    
    void shuffle( card_t * const wDeck );
    
    
    void deal( const card_t * const wDeck );
    
    
     // initialize array of pointers
    char Face[] = {"A23456789TJQK"};
    
    
    int main()
    {
      // define array of cards
      card_t deck[52];
    
    
      card_t *deckptr;
      deckptr = deck;
      //Randomize
      srand( time( NULL ) );
    
    
     //Fill deck with cards
     fillDeck(deckptr);
    
    
     //Arrange Cards in a random order
     shuffle( deck );
    
    
     for(int i = 0; i <=51; i++)
     {
       printf("%c--%c\n",Face[deckptr->Facevalue],deckptr->suitValue);
     }
    
    
    }
    void fillDeck( card_t *deckptr)
    {
      //Deal all cards in the deckck
      for( int i = 3;  i < 7; i ++)
      {
         for( int j = 2; j < 15; j++)
         {
            deckptr->suitValue = i;
            deckptr->Facevalue = j;
         }
         deckptr++;
      }
    }
    
    
    void shuffle( card_t *deckptr)
    {
      card_t temp, *crdptr;
    
    
      for(int i = 0; i < 53; i++)
      {
        int num = rand() % 52;
        crdptr = deckptr + num;
        temp = *crdptr;
        *crdptr = *deckptr;
        *deckptr = temp;
        deckptr + 1;
      }
    }
    The error I keep getting is :
    shuffle.c: In function ‘void fillDeck(card_t*)’:
    shuffle.c:56:28: error: invalid conversion from ‘int’ to ‘suit_t’ [-fpermissive]
    deckptr->suitValue = i;

    I'm not sure exactly hot to fix this problem.
    Thank you again for all your help.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Why are you using bitfields?

    As for you question, maybe you meant suitValue to be an int?

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    3
    This is what my professor gave us :
    Code:
    The suits and cards are to be definedastypedef enum { clubs, diamonds, hearts, spades} suit_t;
    typedef struct{unsigned int number : 4;suit_t suit : 2;} card_t;
    I changed suitValue to an Int and the code complies but my output is:
    A--(52 times one per line)

    Thank you for all your help

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Your prof ain't too bright. But I guess you have to live with it.

    Anyway, if that's what you're supposed to use then it seems you shouldn't add all that extra (unnecessary) stuff to the struct.

    I find your code difficult to understand. Why are the suits being assigned values from 3 to 6? Shouldn't they be 0 to 3 to correspond to the values in suit_t? Why are your (misnamed) "face" values assigned values from 2 to 14? I see some sense in that, but you can't then use those values directly to index your Face string since its indices are from 0 to 12.

    Try getting rid of all the extra junk you added to the struct. Then fill the deck by setting suit from 0 to 3 and number from 0 to 12. Then try fixing up your shuffle a bit (e.g., deckptr + 1; does nothing).

  5. #5
    Registered User
    Join Date
    Apr 2016
    Posts
    3
    Thank you for all your help. I cleaned up the struct and I changed the values in filling the deck. I deleted the deckptr+1; in the shuffle function. I think my problem now lies in the following code:
    Code:
     shuffle( deck );
    
    
     for(int i = 0; i <=51; i++)
     {
       printf("%c--%c\n",Face[deckptr->Facevalue],deckptr->suitValue);
     }

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    The struct that your prof wants you to use only has the members suit and number, so I don't see where Facevalue and suitValue come from. My advice was to get rid of those entirely and use only suit and number.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Search a stuct
    By mrsirpoopsalot in forum C++ Programming
    Replies: 4
    Last Post: 02-25-2009, 08:13 PM
  2. how can I load a file into a stuct
    By timgao in forum C Programming
    Replies: 20
    Last Post: 01-21-2007, 12:46 PM
  3. Queue stuct - Please Help
    By micmac700 in forum C Programming
    Replies: 3
    Last Post: 09-23-2006, 02:05 PM
  4. stuct (or something) help...
    By Rune Hunter in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2004, 04:27 PM