#### Archived

This topic is now archived and is closed to further replies.

# I need help with my black jack game (c++)

This topic is 5237 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, I''m having trouble with dealing the cards correctly. Lemme give you my source first and then I''ll explain my problem.
#include <iostream>
#include <time.h>

int Card[2];
int i = 0;

void SetCards()
{
int x;
srand(time(NULL));
x = rand()%12;
Card[i] = x;
}

void ShowCards()
{
std::cout << "Your deck consists of these cards: " << std::endl;
while(i<3)
{
SetCards();
i++;
}
for(int j=0; j<3; j++)
{
std::cout << "[" << Card[j] << "] ";
}
}

int main()
{
SetCards();
ShowCards();
return 0;
}

This is just a mere prototype. I plan to make it OO becuase I like objects and I''ll make it all ''black jacky'' in due time. The problem I''m having is that whenever I compile and run whatever I have here, the first two ''cards'' are the same exact number despite the seed. How can I go about fixing this? I checked my loops and they seemed fine, maybe I should try using references? They do tend to be faster.
R.I.P. Mark Osback Solo Pa Mi Gente VG-Force

##### Share on other sites
only need to seed the generator once, so in your main function just seed it there.

##### Share on other sites
call srand once, perhaps at the beginning of main.

You''re calling SetCards 3 times succesively so fast that the seed (time) doesn''t change.

[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people

##### Share on other sites
I had a feeling it had to deal with that. Thanks a bunch guys!

R.I.P. Mark Osback
Solo Pa Mi Gente
VG-Force

##### Share on other sites
<pre>
while(i<3)
{
SetCards();
i++;
}
</pre><br>
Your array is going out of range if you do this and could cause some problems set it to while(i<2) or something like that so it doesn''t.

Also this line ''x = rand()%12;'' will generate a number from 0 to 11 you probably want something like ''x = rand()%12+1'' that genereates a number from 1-12.