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;
}
}
I can't believe I was using count as my iterator... location was what I intended to use. And since I started count at 2, hand should be 13. Sometimes you just need to take a break and come back to catch the errors. This works fine:
void initCards(){
int count = 0;
int location = 0;
const int hand = 13;
//add hearts
int value=2;
while( count < hand ){
struct card temp = {"Hearts", value, false};
cards[location] = temp;
value++;
location++;
count++;
}
count = 0;
//add diamonts
value = 2;
while( count < hand ){
struct card temp = {"Diamonds", value, false};
cards[location] = temp;
value++;
location++;
count++;
}
//add spades
count = 0;
value = 2;
while( count < hand ){
struct card temp = {"Spades", value, false};
cards[location] = temp;
value++;
location++;
count++;
}
//add clubs
count = 0;
value = 2;
while( count < hand ){
struct card temp = {"Clubs", value, false};
cards[location] = temp;
value++;
location++;
count++;
}
for(int i=0; i<52; i++){
cout << cards[i].type << " " << cards[i].rank << endl;
}
}