Public Group

c++ Infinate loop help

This topic is 2615 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi. I just tried making a program that let's you pick a number and then the computer will find it, but I'm getting stuck in an infinate loop and I need some help.
 #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { int maxnumber = 100; int minnumber = 1; int number; int tries = 0; int answer; srand(static_cast<unsigned int>(time(0))); int randnumber = rand() & maxnumber + minnumber; cout << "Pick a number\n"; cin >> number; do{ int randnumber = rand() & maxnumber + minnumber; if(randnumber > number){ cout << randnumber << endl; maxnumber = randnumber; ++tries; } else if(randnumber < number){ cout << randnumber << endl; minnumber = randnumber; ++tries; } else { randnumber = answer; cout << "The number is " << randnumber << ". I made it in " << tries << " tries"; } }while(answer != number); system ("pause"); return 0; } 

Share on other sites
Switch

Into

(in the else condition)

Share on other sites
Thx, but it's still not working properly
 else { answer = randnumber; cout << "The number is " << randnumber << ". I made it in " << tries << " tries"; } 

Share on other sites
Once the do loop is entered the user is never again asked for an answer. So, since the do loop continues until the correct answer is given and the user is only once allowed to provide one...

Also, I'm a little confused by this game. Why is randnumber initialized/used twice? It is initialized before the do loop then another randnumber is initialized upon entering the do loop. The randnumber within the do loop would seem to be creating a moving target for the user, if the user were allowed to guess. This seems somewhat different than the typical version of this game.

Share on other sites
Thx for the help, don't know why I had the "answer" variable at all. But what should I do to make this work?
And if you red the introduction at the start of this thread you would see that the point of this game is that you pick a number and the computer guesses it.
Just somthng I wantet to try out

Share on other sites
Put your cin line inside the loop. Don't have the option to test it myself but this should do it.

Share on other sites
One potential problem your may have is that you never initialize answer. The only time it is assigned a value in your code, is if the answer is guessed correctly. If your running in debug mode, you might be lucky and it gets assigned 0. This is not a guarantee in release code. Your do/while loop hinges on the fact that answer != number, the problem being that answer could technically be anything

Second: (might be intentional) is you are creating an entirely new randnumber inside the scope of your do/while. Since you generate a random number in each loop, you can remove the one before the loop.

Third: I don't think your random number guesser is ever going to give you the number you want. Typically to clamp the top value of a value, you can use the modulus operation (%). For example random % 100, gives you a range of 0 - 99. Do an internet search about creating a random number within a range.

 int range = maxnumber - minnumber + 1; int randnumber = (rand() % range) + minnumber; 

Forth: The problem with your version is you are adding the lower bounds in, which will frequently put it over the upper bounds. Also your two bounds can move in any direction. You should make it so it only goes in the direction of your number (min only goes up, max only goes down).

 if(randnumber > number) { cout << randnumber << '\n'; maxnumber = std::min(maxnumber, randnumber); ++tries; } else if(randnumber < number) { cout << randnumber << '\n'; minnumber = std::max(minnumber, randnumber); ++tries; } 
^^ Note that if your range code is working properly the min/max tests are probably unnecessary.

Fifth: Termination. Kind of hit on this earlier, as far as using answer. I would remove the inequality check between answer and number and replace it with a bool. Make a bool variable called something like Complete and initialize it to false. Then only set it to true once the number has been guessed. Another option would be to make an infinite loop (while(true)) and put a break; at the end of the else block.

Added some code examples Edited by Rattrap

Share on other sites
Hidden
Thx again, but if I do it this way I will have to input the same number several times for the computer to find it.
However, this doesn't work either, but that must be an other error in my program.
I

Thank you. I'll se if I can get it working now

1. 1
2. 2
Rutin
19
3. 3
4. 4
khawk
15
5. 5

• 13
• 26
• 10
• 11
• 44
• Forum Statistics

• Total Topics
633743
• Total Posts
3013644
×