Thread: error expected identifer or "(" beore " . " token

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    error expected identifer or "(" beore " . " token

    i am trying to write a poker game. the rules i have set myself are no functions over 30 lines and no global delectations. i want anything to do with the "deck of cards" to be separate from main but im just twisting myself into tighter and tighter knots.

    now i have the above error
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    enum Suit { SPADES = 0, HEARTS = 1, CLUBS = 2, DIAMONDS = 3};
    enum Card_Value {TWO =2, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE};
    typedef struct
    {
        enum Suit suit;
        enum Card_Value card;
        char card_code[52];
    } Card;
    typedef struct
    {
        char player_name[10];
        Card hand[5];
    }Hand;
    
    void initalize_random_generator(void);
    void shuffle_deck(Card *p_deck_index);
    void deck(void);
    char * get_card_code(int suit, int face_value);
    void deal_cards(int player_num, int num_of_cards, Card *p_deck_index);
    
    int main()
    {
        int num_of_players;
        int i, j;
    
        initalize_random_generator();
        deck();
    
        printf("Enter the number of players: ");
        scanf(" %d", &num_of_players);
        // declare variable player array
        Hand player[num_of_players];
    
        for (i = 0; i < num_of_players; i++)
        {
            printf("Please enter player %d's name: ", i + 1);
            scanf(" %s", player[i].player_name);
        }
    
    }
    
    void initalize_random_generator(void)
    {
        srand((unsigned) time(NULL));
    }
    
    void deck(void)
    {
        static Card deck[52];
    
        shuffle_deck(deck);
    }
    
    void shuffle_deck(Card *p_deck_index)
    {
        int count_cards = 0, card_found = 0;
        int i;
        int suit_value, face_value;
        char *temp_string;
    
        while (count_cards < 52)
        {
            suit_value = rand() % 4;
            face_value = rand() % 13 + 2;
            for (i = 0; i < count_cards + 1; i++)
            {
                if (p_deck_index[i].suit == suit_value && p_deck_index[i].card == face_value)
                {
                    card_found = 1;
                    break;
                }
            }
            if (!card_found) //card_found is 0
            {
    /*
                p_deck_index[count_cards].suit = suit_value;
                p_deck_index[count_cards].card = face_value;
                temp_string = get_card_code(suit_value, face_value);
                strcpy(p_deck_index[count_cards].card_code, temp_string));
                count_cards ++;
    //*/
                temp_string = get_card_code(suit_value, face_value);
                p_deck_index[count_cards] = (Card) {suit_value, face_value};
                strcpy(p_deck_index[count_cards++].card_code, temp_string);
            }
            card_found = 0;
        }
        for (i = 0; i < 52; i++)
        {
            printf("%s ", p_deck_index[i].card_code);
        }
    }
    
    char * get_card_code(int suit, int face_value)
    {
    /*******************************************
     * Thanks to flp1969 for this code. Suit:  *
     * goes from  0 spades, 1 hearts ,2 clubs, *
     * and 3 is diamonds. Card: ace = 0, 2 = 1,*
     * 3 = 2 ... 10 = 9, J = 10, Q = 11, K = 12*
     *******************************************/
    
        static char s[5] = { 0 };
    
        int c = 0x1f0a1;
    
        if (face_value == ACE)
        {
            face_value -= 14;
        }
        else
        {
            face_value -= 1;
        }
    
        switch ( suit )
        {   //deliberate fall through
            case 2: // clubs
                c += 0x10;
            case 3: // diamonds
                c += 0x10;
            case 1: // hearts
                c += 0x10;
        }
    
        switch ( face_value )
        {
            case 0 ... 10:
                c += face_value;
                break;
            default:
                c += face_value + 1;
        }
    
        s[0] = 0xf0 | ((c >> 18) & 7);
        s[1] = 0x80 | ((c >> 12) & 0x3f);
        s[2] = 0x80 | ((c >> 6) & 0x3f);
        s[3] = 0x80 | (c & 0x3f);
    
        return s;
    }
    
    void deal_cards(int player_num, int num_of_cards, Card *p_deck_index)
    {
        int i, j;
        for (j = 0; j < player_num; j++)
        {
            for (i = 0; i < num_of_cards; i++)
            {
                Hand.hand[i] = Card[p_deck_index];
            }
        }
    }
    hand is supposed to hold 5 cards
    coop

  2. #2
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    i have thought about what im actually trying to do. ie had a cig and a cuppa the function deal_cards is utter rubbish it doesn't help me in the slightest the new function is
    Code:
    void deal_cards(int player_num, int num_of_cards, int num_of_cards_in_hand, Card *p_deck_index, Hand player[])
    {
                int i;
    
                for (i = 0 + num_of_cards_in_hand; i < num_of_cards; i++)
                {
                    player[player_num].hand[i] = Card[p_deck_index];
                }
    }
    but now i get an error expected expression before Card
    sorry to mess about
    coop

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    am i going to have to let main have access to the deck pointer??

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Isn't p_deck_index a pointer, not an index?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    yes it is it points to deck. but deck is an array so i think of it as an index ie p_deck_index[0] points to the first element of deck

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    my thinking was i could have the call to the function something like deal_card(........... cards_dealt....)

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    the function call would be
    Code:
      for (i = 0; i < num_of_players; i++)
        {
           deal_cards (i, 1, 0, cards_dealt, player)
        }
    but im guessing this wont do
    coop
    Last edited by cooper1200; 05-11-2019 at 08:02 AM.

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Assuming you provide the pointer to the Hands, and number of players, as well of number of cards per player:

    Code:
    void deal_cards( Card **deckpp, Hand *handp, unsigned int nplayers, unsigned int ncards )
    {
      unsigned int i, j;
      Card *p;
    
      p = *deckpp;
      while ( nplayers-- )
      {
        for (j = 0; j < ncards; j++ )
          handp->hand[j] = *p++;
    
        handp++; // next player hand.
      }
    
      // update *deckpp so next deal start there.
      *deckpp = p;
    }
    To call the function simply do:
    Code:
    Code *tmp = deck; // deck is your array of cards...
    Hand hands[2];
    
    deal_cards( &tmp, hands, 2, 5 ); // deal 5 cards to 2 player's hands and advance tmp for the next deal...
    // Here, now, tmp points to the next card to be dealt...
    Of course, somewhere you need to verify array bounds...
    Last edited by flp1969; 05-11-2019 at 08:24 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cooper1200
    yes it is it points to deck. but deck is an array so i think of it as an index ie p_deck_index[0] points to the first element of deck
    I asked because you seem to be quite literally trying to use it as an index: Card[p_deck_index]. That looks like Card is an array and p_deck_index is an integer array index, but of course Card is an alias for a struct type and p_deck_index is a pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-21-2012, 11:50 PM
  2. Replies: 10
    Last Post: 08-09-2012, 12:48 PM
  3. Replies: 9
    Last Post: 03-31-2009, 04:23 PM
  4. Replies: 7
    Last Post: 09-14-2008, 08:37 AM
  5. Replies: 2
    Last Post: 06-29-2007, 07:55 AM

Tags for this Thread