The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


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:

Closed Thread
 
LinkBack (1) Thread Tools    Display Modes   
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 06-10-2007, 04:19 PM
Trikky73
Posts: n/a
[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!!!!!!!!!!

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
  #2 (permalink)  
Old 06-10-2007, 04:19 PM
thor78
Posts: n/a
I'm guessing the variables Remainder and move are integers. Try declaring them double instead.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
  #3 (permalink)  
Old 06-10-2007, 04:19 PM
AshB
Posts: n/a
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.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Closed Thread


Thread Tools
Display Modes

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

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

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


All times are GMT -7. The time now is 05:34 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50