Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 03-05-2003, 04:18 PM   #1
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
C++ problem

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
Attached Files
File Type: zip lab3b.zip (2.0 KB, 35 views)
GSXdan is offline   Reply With Quote
Old 03-05-2003, 04:41 PM   #2
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
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.
doctorgonzo is offline   Reply With Quote
Old 03-05-2003, 05:19 PM   #3
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
Thank you so much doctorgonzo!! You just saved me about 3 hours of pulling my hair out

Thanks again ^dan
GSXdan is offline   Reply With Quote
Old 03-05-2003, 05:39 PM   #4
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
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
GSXdan is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 12:31 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2