![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| Tags: |
![]() |
![]() | | LinkBack (1) | Thread Tools | Display Modes | ![]() |
| |||
| [SOLVED] C++ Programming Code Problem? I have this bit of code - that doesn't end - when run the program just hangs at this point - any suggestions?? do { for (double x = 2.0 ; x <= 6.0; x++) { Remainder = pow(2,x) - 1; move = Pile - Remainder; } } while(move <= Pile/2); I get an error on compiling that pow() is doesn't like "pow(int, int)" so I have the x as a double - but then it gives an error saying that it's trying to convert an int from a double.....help - it's the last little bit of a program that I have spent ages writing............please!!!!!!!!! No move always has to be between 1 and half the pile - this is what is considered a "legal" move. If I don't write the loop like that how else can it be written to find a value for remainder that makes the move legal? Ok I've been trying different loops to make it work and I have one that seems to work - but it should be giving me random answers but it's not - any suggestions? BTW in the REAL program - the amount of the Pile will be a randomly generated number (RandomInt () ) or the number left after the last turn: int Pile = 70; int Remainder; int move; for ( ![]() { RandomInt Smart(2,6); int x = Smart.generate(); Remainder = pow(2.0,x) - 1; move = Pile - Remainder; if ( move >= 1 && move <= Pile/2 ) break ; } cout << "move = " << move << endl; Pile -= move ; cout << "Pile = " << Pile << endl ; NEVER MIND I WORKED OUT WHY I GET THE SAME ANSWER - I FELL STUPID SOMETIMES!!!!!!!!!! |
| |
| |||
| Run it through a debugger, and watch the values of move and Pile change. Is move ever great than or equal to half of Pile? Also, why have you got the for-loop inside a do-loop? That do-loop will run only once, or forever, depending on what the value of Pile is. ----------- EDIT: Assuming you have got all the ints and doubles correctly sorted and consistant... The code will enter the do loop, and then enter the for loop. The for loop will then, after 5 iterations, set move to (Pile - 63). Then the while condition will execute, and if Pile is greater than 126, the do loop will start again. Otherwise, it will finish. You might as well write: do{ Remainder = 63.0; move = Pile - 63.0; }while(move <= Pile/2); As it would give *exactly* the results. |
![]() |
| Thread Tools | |
| Display Modes | |
| |
LinkBacks (?)
LinkBack to this Thread: http://www.programmerstalk.net/thread193.html | |||
| Posted By | For | Type | Date |
| Don't Hire Forum Posters! Get Unlimited Posts On Your Forum For 45$ - Webmaster Forum | This thread | Refback | 06-24-2007 11:01 AM |