Hey I have a blackjack program and I'm trying to add a card to the players hand when choosing to hit. But for some reason my function to add a card to hand makes the program stop. it doesn't close or anything it will just stop.
1 2 3 4 5 6 7 8 9 10
if(hit=='y')
{
nextCard=getTopCard(deck);
cout << "your delt the "<<endl;
showCard(nextCard);
addToHand(userDeck, nextCard);
cout<<"\nyour hand is now :"<<endl;
showHand(userDeck, 10, false);
cout<<endl;
}
1 2 3 4 5 6 7 8 9 10 11
void addToHand(int hand[], int cardToAdd)
{
int i=0;
while (hand[i] != 0);
{
i++;
}
hand[i]=cardToAdd;
}
You'd want to remove the semicolon on the while loop condition line (line 5 of addToHand)
Is one of the values in the hand array guaranteed to be 0 so that the loop stops? You keep incrementing i and I can see the possibility of going out of bounds on the array and running an infinite loop since i isn't strictly bound to the size of the array.
The semi colon stops the loop there. Maybe move line 10 inside the loop? Wildblue definitely knows more than me as he is helping me with my problems lol, but just my opinion.
Hey guys thanks for the responses. wildblue thanks I did not notice that semicolon and removed it and now it works, And yes the hand array does have 0's because the cards are added into it so all indexes are 0 from start. and justinc I tried that but it still ended the program. Thanks again for the help guys. it now doesn't stop but it still not doing what I want it to. Gotta put more into it.