Jump to content
  • Advertisement
Sign in to follow this  
sniffbaron

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Thx, but it's still not working properly


else {
answer = randnumber;
cout << "The number is " << randnumber << ". I made it in " << tries << " tries";

}



Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

[edit]
Added some code examples Edited by Rattrap

Share this post


Link to post
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

Share this post


Link to post
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!