![]() |
C++ Getting stuck within a loop.
So I took a break from programming for a few months and am back at it. I am attempting to make a function that will shuffle a deck of cards (52) to 4 players. I have setup a 2d array (myDeal[4][52]) and set all values to 0 for the time being. I select the random card by using rand()%52. If the value is available for the current player, ie myDeal[1][22] I am setting the value to 1 to show that it is there. The problem I am having is that my code seems to be getting stuck within a loop for no reason that I can see, but like I said I am a bit rusty on my programming currently.
:
void myDeal()I know I could use just one If, but since it has been giving me problems, I have broken it up for now. It's just driving me insane, so any little help you can give is much appreciated. Thanks |
You never actually increment "dIndex" and "pIndex". Just change "dIndex" to "d" and "pIndex" to "p".
:
|
Ok, that was my bad I was editing the code for web is all and missed that. In my code the variables are the same. The problem still occurs when the variables are correct, sorry for the messup on my end with posting the code.
Here is the code again with the typo fixed. :
void myDeal() |
I think your problem is the fact that by the time you hit 13 you have dealt all your cards out, you have to remember that there are only 52 cards. So if for each time through the parent for loop you are dealing 4 cards by the time you hit 13 you have dealt out all 52 cards, so you will no longer have a free card to deal out. So if you change if from 52 to 13 you will finish the loop and deal all 52 cards out to the 4 players. :)
edit: Currently your function wants to deal out 208 cards, just wanted to point that out. ;) |
Ahh!! I got so stuck up on the fact it was a problem with my syntax or an extra loop or something I didn't think about my logic! Thank you for that observation and it is workin once again.
Again, thank you for helping me with such a stupid mistake, I will make sure to double check my logic next time. Thanks |
Yeah, I actually spent more time on it, but then I compiled it and printed out the stuff and it was getting stuck in the 14th loop or when d was 13 so thats how I figured it out.
|
| All times are GMT -5. The time now is 3:00 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC