Loans | Loan | هيفاء وهبي | Myspace Layouts | Credit Cards
C++ problem [Archive] - PCMech Forums

PDA

View Full Version : C++ problem


GSXdan
03-05-2003, 05:18 PM
I need to write a program that is like the game Chutes and Ladders, and I am getting an infinite loop and I know it is because of the function call in the while loop header, but my teacher said we had to put it there. Any ideas about why it does not work?

thanks ^dan

doctorgonzo
03-05-2003, 05:41 PM
Here are the problems I found:

1) You initialized movenumber inside of the while loop. Thus, at the beginning of the loop movenumber always went back to 1. I move it before the while loop.

2) You forgot the number 6 in your RollDice function. I changed it to diceroll = rand() % 6 +1;

3) I changed the Move function to take positionX by reference. Before, there was no way to know what the position was after calling the Move function, because you didn't save the return value from Move into any variable. You just called it. The solution was to either pass positionX by reference and return nothing, or save the return value somewhere. I did the former.

4) In the PrintMove function, you reset ct to 0 when it reached 3. That caused it to print the line break every four lines instead of every three. I changed it to reset the value to 1.

It seems to work now, although it isn't random: C always wins for me, 69-70-83. You will probably want to change that yourself.

GSXdan
03-05-2003, 06:19 PM
Thank you so much doctorgonzo!! You just saved me about 3 hours of pulling my hair out:)

Thanks again ^dan

GSXdan
03-05-2003, 06:39 PM
Oh yea and how you said that C won all the time, I needed to add this code: srand(time(NULL)); to get a truely random number.

Thanks again ^dan