So basically I have an array of Card objects initialized like so…
Card *pile[MAX];
where MAX is 52. This array is a member of a class called CardPile
The problem I’m having occurs when I try to fill an instance of CardPilecalled drawPile to its 52-element capacity by using a for-loop…
This is how I chose to do that:
char faces[] = "A23456789TJQK";
char suits[] = "SHDC";
int k = 0;
for(int i=0; i<13; i++){
for(int j=0; j<4; j++){
drawPile.add(new Card(faces[i], suits[j]));
//the line below is displayed on the console for debugging purposes
cout << drawPile.pile[k]->face << drawPile.pile[k]->suit << ", pile size=" << drawPile.size << endl;
k++;
}
}
and here is the code for the .add(Card*) method…
void CardPile::add(Card *c){
for(int i=0; i<52;i++){
if(pile[i]){
continue;
} else {
this->pile[i]=c;
size++;
break;
}
}
}
…and when I run the program, I get this displayed in the console…
AS, pile size=1
AH, pile size=2
AD, pile size=3
AC, pile size=4
2S, pile size=5
2H, pile size=6
2D, pile size=7
2C, pile size=8
3S, pile size=9
3H, pile size=10
3D, pile size=11
3C, pile size=12
4S, pile size=13
4H, pile size=14
4D, pile size=15
4C, pile size=16
5S, pile size=17
5H, pile size=18
5D, pile size=19
5C, pile size=20
6S, pile size=21
6H, pile size=22
6D, pile size=23
6C, pile size=24
7S, pile size=25
7H, pile size=26
7D, pile size=27
7C, pile size=28
8S, pile size=29
8H, pile size=30
8D, pile size=31
8C, pile size=32
9S, pile size=33
9H, pile size=34
9D, pile size=35
9C, pile size=36
TS, pile size=37
TH, pile size=38
TD, pile size=39
After that last line of output, the program stops running… My question is, why does the program stop after the 39th call to add() instead of finishing and filling the array to its allocated capacity?
Thanks in advance
You need to initialize piles element, it default has random value:
Also you could try to use std::vector instead of array with raw pointer