1. ## question about my code

im working a card program that will shuffel and display the cards. im trying to randomize numbers but this code will not print anything. it seems like it goes into an endless loop.

for(int g=0; g<52; g++)
{
do
{
card1 = rand() % 52;

for(int d=0; d<52; d++)
{
if(card_array[d]==card1)
counter++;
}
if (counter > 4)
{
u=0;
}
else if (counter < 4)
{
card_array[g] = card1;
u=1;
}
}while(u==0);

cout << card1 <<endl;

}

any help would be good.

2. Output counter and u and re-examine you conditions and I bet you'll be able to find your problem.

/*Edit */
HINT: Think about setting counter back to zero during the loop.

3. and i what i got was an endless loop. gotta work that out!

4. im still having much trouble. I want the rand to generate a number btw 0-52. and check its value with previously generated values that are stored in array. if a match occurs more than 4 times , randomize again. if there isnt, store it in the next array "window". But the code doesnt work. I understand the logic behind it and i tried to put into code but it doenst translate into workable code.

5. good point with the counter. let me try that

6. ive tried re-working the code, but i keep coming with idea. it it randomizes but it still will intsert the value in the array even thoughh counter>4. when i try to output the array of values, it returns numbers out of the range of 52 and sometimes negative values. im guessing its the address, but im not telling it to store the address.

7. I did this with randgen

int DealCard(apvector<int> & cards)
{
RandGen r;
int value;
do
{
value = r.RandInt(52);
} while(cards[value] != 1);
cards[value] = 0;
return value;
}

void PrintCard(int card)
{
switch (card%13) {
case 0: cout<<"2 of "; break;
case 1: cout<<"3 of "; break;
case 2: cout<<"4 of "; break;
case 3: cout<<"5 of "; break;
case 4: cout<<"6 of "; break;
case 5: cout<<"7 of "; break;
case 6: cout<<"8 of "; break;
case 7: cout<<"9 of "; break;
case 8: cout<<"10 of "; break;
case 9: cout<<"Jack of "; break;
case 10: cout<<"Queen of "; break;
case 11: cout<<"King of ";break;
case 12: cout<<"Ace of "; break;
}
switch (card / 13) {
case 1: cout<<"hearts. "; break;
case 2: cout<<"clubs. "; break;
case 3: cout<<"diamonds. "; break;
}
}

You mod the cards to get the value and you divide to get the suit....

8. How about a different approach to shuffling the deck?
Initialize the deck in order (like a fresh deck).
Then, "shuffle" the cards for a couple seconds by swapping two random indices.
Output the deck using Unregistered's idea.

Here's a sample program: