Array of Structs Initialization....

Posted by user69514 on Stack Overflow See other posts from Stack Overflow or by user69514
Published on 2010-05-10T21:06:53Z Indexed on 2010/05/10 21:14 UTC
Read the original article Hit count: 198

Filed under:
|
|
|
|

Hi I am working on a program where I have to initialize a deck of cards. I am using a struct to represent a card. However I'm not filling it correctly as I get a bunch of zero's when I display the deck of cards. I believe my mistake is in this line but I'm not sure:

struct card temp = {"Clubs", value, false};

The code:

void initCards(){

    int count = 0;
    int location = 0;
    const int hand = 12;

    //add hearts
    int value=2;
    while( count < hand ){
        struct card temp = {"Hearts", value, false};
        cards[location] = temp;
        value++;
        count++;
    }

    count = 0;

    //add diamonts
    value = 2;
    while( count < hand ){
        struct card temp = {"Diamonds", value, false};
        cards[count] = temp;
        value++;
        count++;
    }

    //add spades
    count = 0;
    value = 2;
    while( count < hand ){
        struct card temp = {"Spades", value, false};
        cards[count] = temp;
        value++;
        count++;
    }

    //add clubs
    count = 0;
    value = 2;
    while( count < hand ){
        struct card temp = {"Clubs", value, false};
        cards[count] = temp;
        value++;
        count++;
    }

    //print the deck
    for(int i=0; i<52; i++){
        cout << cards[i].type << " " << cards[i].rank << endl;
    }
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about struct