Thread: having trouble with structs

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    3

    having trouble with structs

    Hey, I have to make the Irish card game "25" in C Programming. i cant figure out how to print my structs as i still have little experience in this language. If anyone could lend me a hand, it would be appreciated.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    
    //constants declared
    #define NUMOFCARDS 52
    #define NUMOFPROPERTIES 2
    #define NUMOFSUITS 4
    #define NUMOFFACES 13
    #define PLAYERCARDS 5
    #define FIRSTTWOATATIME  3
    
    
    struct playerCards
    
    
    {
    
    
        int playersCardDeck[PLAYERCARDS][NUMOFPROPERTIES];
    
    
        // Above array denotes number cards assigned to a players where represents "PLAYERCARDS" suit and "NUMOFPROPERTIES" represents faces.
    
    
    };
    
    
    //declaration of arrays
    char* suit[NUMOFSUITS] = { "Hearts","Spades","Clubs","Diamonds" };
    char* face[NUMOFFACES] = { "Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine",
    "Ten","Jack","Queen","King" };
    
    
    //calling functions
    void PrintCard(int deck[NUMOFCARDS][NUMOFPROPERTIES], int i);
    void InitDeck(int deck[NUMOFCARDS][NUMOFPROPERTIES]);
    void SwapCards(int deck[NUMOFCARDS][NUMOFPROPERTIES], int src, int dest);
    void ShuffleDeck(int deck[NUMOFCARDS][NUMOFPROPERTIES]);
    
    
    
    
    int main()
    {
        //declaration of varaiables
        int deck[NUMOFCARDS][NUMOFPROPERTIES];
        int player1Deck[5];
        int src = 0;
        int dest = 0;
        int i;
        int j;
        int row=0;
        int players;
        int numPlayers = 0;
        int playerDeck[30];
        srand(time(NULL));
        InitDeck(deck);
        ShuffleDeck(deck);
        SwapCards(deck, src, dest);
        printf("Please enter the number of players (From 1 to 6): \n");
        scanf("%d", &numPlayers);
    
    
        for (players = 0; players < numPlayers; players++)
        {
            printf("Player %d\n", players + 1);
            for (i = 0; i < PLAYERCARDS; i++)
            {
                PrintCard(deck, i);
    
    
                
    
    
    
    
            }
            printf("\n");
            
        }
    
    
    
    
        struct playerCards *userEnteredPlayerCount = NULL;
    
    
        userEnteredPlayerCount = (struct playerCards *)malloc(numPlayers * sizeof(struct playerCards));
    
    
        {
    
    
            for (players = 0; players < numPlayers; players++) // assign two cards at a time to all players
    
    
            {
    
    
                for (i = 0; i < (PLAYERCARDS-FIRSTTWOATATIME); i++)
    
    
                {
    
    
                    for (j = 0; j < NUMOFPROPERTIES; j++)
    
    
                    {
    
    
                        userEnteredPlayerCount[players].playersCardDeck[i][j] = deck[i][j];
    
    
    
    
                    }
    
    
                    row++; //indicates first rows elements are assigned to a player
    
    
                }
    
    
            }
    
    
            for (players = 0; players < numPlayers; players++) // assign next three cards at a time to all players
            {
    
    
                /* row variable is updated above which keeps updated with which row of elements are assigned to a player and this ensures no duplicat entries are assigned to a player */
    
    
                for (i = row; i < PLAYERCARDS; i++)
    
    
                {
    
    
                    for (j = 0; j < NUMOFPROPERTIES; j++)
    
    
                    {
    
    
                        userEnteredPlayerCount[players].playersCardDeck[i][j] = deck[i][j]; // assign the cards from deck variable
    
    
                    }
    
    
                }
    
    
            }
    
    
        }
    
    
        return 0;
    
    
        printf("%d", userEnteredPlayerCount->playersCardDeck);
    
    
        
    
    
    }
    
    
    void InitDeck(int deck[NUMOFCARDS][NUMOFPROPERTIES])
    {
        int suit;
        int face;
        int row = 0;
        for (suit = 0; suit < 4; suit++)
            for (face = 0; face < 13; face++)
            {
                deck[row][0] = suit;
                deck[row][1] = face;
                row++;
    
    
                
            }
    }
    void ShuffleDeck(int deck[NUMOFCARDS][NUMOFPROPERTIES])
    {
        int src, dest, i;
        srand(time(NULL));
    
    
        for (i = 0; i < NUMOFCARDS; i++)
        {
            src = i;
            dest = rand() % NUMOFCARDS;
            SwapCards(deck, src, dest);
    
    
        }
    }
    
    
    void SwapCards(int deck[NUMOFCARDS][NUMOFPROPERTIES], int src, int dest)
    {
        int temp;
        temp = deck[src][0];
        deck[src][0] = deck[dest][0];
        deck[dest][0] = temp;
        temp = deck[src][1];
        deck[src][1] = deck[dest][1];
        deck[dest][1] = temp;
    
    
    }
    void PrintCard(int deck[NUMOFCARDS][NUMOFPROPERTIES], int i)
    {
        int tempsuit;
        int tempface;
        tempsuit = deck[i][0];
        tempface = deck[i][1];
        printf("Card %d = %s of %s\n", i+1, face[tempface], suit[tempsuit]);
    
    
    }
    Attached Images Attached Images

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,211
    Why not start with.
    Code:
    struct card {
        int rank;
        int suit;
    };
    
    struct card deck[52];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,683
    I would like to point out that CONSTANTS aren't always the best approach, especially since seeing that your arrays are global. I know, they've told you to avoid magic numbers and that is correct, but in C there are other ways to do the same thing, without using constants. For example:
    Code:
    int array[] = { 1, 3, 2, 4, 6, 5, 8, 7, 9 };
    
    int arrayAmount = sizeof(array)/sizeof(*array);
    
    // The compiler knows the size so you don't have to
    // This of course only works with static arrays, not with pointers
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble understanding malloc + structs
    By Finoli in forum C Programming
    Replies: 15
    Last Post: 01-03-2016, 07:45 AM
  2. Having trouble with structs
    By C0__0D in forum C Programming
    Replies: 9
    Last Post: 02-05-2013, 11:43 PM
  3. Trouble with reading in data to an array of structs
    By ljgerr93 in forum C Programming
    Replies: 6
    Last Post: 01-22-2012, 01:57 PM
  4. Replies: 7
    Last Post: 06-04-2008, 10:39 PM
  5. trouble with structs..
    By cs32 in forum C Programming
    Replies: 1
    Last Post: 01-22-2008, 11:48 PM

Tags for this Thread