trying to populate an element of array.

Hey so I have a black jack program and I'm trying to set up a part if user wants to hit. I have an array for the deck, user deck (hand), and dealer deck (hand). I have functions to get the top card from deck which works, another to show the card, which works, a add to hand function to update the hand, and a show hand function that would work if the add to hand function did. The add to hand function is not putting the next card value into the next element of the user deck array. I just cannot contemplate why this wouldn't be working. Any help would be greatly appreciated.


here is the part that I'm calling functions to calculate the new hand when user chooses to hit.
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<<userDeck[2];
	cout<<"\nyour hand is now :"<<endl;
	showCards(userDeck, 10, false);
    cout<<endl;



here is my get top card function, it finds the first non 0 element and replaces it with 0 and returns that top card.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int getTopCard(int deck[])
{
    int top = 0;

    for (int i = 0; i <= 52; i++)
    {
        if (deck[i] != 0)
        {
        top = deck[i];
        deck[i] = 0;
		return top;
        }
    }
}


this is my add to hand function, it is suppose to find the first non 0 element in the user/dealer deck and replace it with the next card.
1
2
3
4
5
6
7
8
9
10
  void addToHand(int hand[], int cardToAdd)
{
	int i=0;

    while (hand[i] != 0)
    {
        i++;
    }
	hand[i]=cardToAdd;
}



this function will just output each card in the user deck array using another function that outputs the value and suite.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void showCards(const int cards[], int numCards, bool hideFirstCard)
{
    //if first card display ** in place of first card and start array at [1]
    if (hideFirstCard==true)
    {
        cout << "**" << " ";
        
        for(int a = 1; a <= numCards-1; a++)
        { 
            showCard(cards[a]);
            cout << " ";
        }
    }

    if (hideFirstCard == false)
    {
        for(int a = 0; a <= numCards-1; a++)
        { 
            showCard(cards[a]);
            cout << " ";
        }
    }
}
Last edited on
Topic archived. No new replies allowed.