Need help adding value to first 0 element in array

Hey so I have a black jack program and I am trying to set up a function to take the next card in the deck and add it to the users hand. This function is suppose to skip all the non zero elements then add the next card into the first 0 element it finds. For some reason it is just not adding the value into the array. I'll post the function I'm using and if needed i can post how I'm calling it. Any help on this is greatly appreciated.
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;
}
Post how you're calling the function. Unless I'm having a complete brain fart, your code looks ok.
Thanks for the reply I appreciate it. so I have a function to get the top card off deck. Then I have that equal nextCard. I use the showCard function to output it. Then I'm suppose to use the addToHand to add the nextCard to the userDeck array and showCards will output the new hand. When I build and try it out however it doesn't add nextCard to the user deck and just outputs the original hand again. I know that the other functions work because when I manually set the next element in the userDeck array to nextCard it outputs correct.

1
2
3
4
5
6
7
8
9
	if (hit=='y'||hit=='Y')
	{
	nextCard=getTopCard(deck);
	cout << "your delt the "<<endl;
	showCard(nextCard);
	addToHand(userDeck, nextCard);
	cout<<"\nyour hand is now :"<<endl;
	showCards(userDeck, 10, false);
    cout<<endl;
What does the "showCards" function look like? Your problem might be there if it isn't outputting the hand properly.
here it is. I'm pretty sure it is working because when I just set userDeck[2]=nextCard and don't use addToHand the showCards outputs correct. Also I just tried outputting userDeck[2] after using addToHand and it gives -858993460.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* ==================================================
Name: showCards
Desc: displays each card in the given array
Parameters: card[] (const int)-array of the hand, 
            numCards (int)- number of cards in hand, 
            hideFirstCard (bool)- determine if first card, if so hide
Return:
   void
================================================== */
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 << " ";
        }
    }
}
Having the value -858993460 (or similiar values) happens sometimes when going out of bounds on an array, or when not initializing values.
In your while loop, make sure to check if you are going over the number of cards available.

1
2
3
4
5
6
7
8
9
10
11
12
13
  void addToHand(int hand[], int cardToAdd)
{
	int i=0;

    while (hand[i] != 0)
    {
        i++;
        if (i >= max)
            break; //don't want to go out of bounds
    }
	hand[i]=cardToAdd; //if i is higher than the max allowed for the array, 
        //we will go out of bounds, and that's no fun
}


Double check that to make sure it isn't going out of bounds.
Last edited on
Topic archived. No new replies allowed.