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

## Recommended Posts

I am doing an excersize in a c++ book that I have which is to reverse the roles of a simple "guess my number game" i.e I am trying to program the computer to guess a number I input. I could probably easily write a program in which the computer randomly guesses a number between 1 and 100 every time but I am trying to add more user interaction by allow the user to say "higher" or "lower" after each guess. Now, say the number to guess is 35 and the computers first guess is 40 (the user says lower) then the next guess is 30 (the user inputs higher) I am struggling to get the computer to remember the 40 as well as the 30 so that the formula for the next guess can generate a random number between 30 and 40. Any help appreciated Cheers P.S I will post some of my code later, (at work atm)

##### Share on other sites
What you have to do is clip the range of numbers that it could possibly be.

For example:

0 <= X <= 100

"i guess 30"
"sorry, its higher"

new range:

30 <= X <= 100

"i guess 70"
"sorry its lower"

30 <= X <= 70

And so forth.

The best way of having the computer guess is to find the middle number of the range and have the computer guess that. For example the computer would guess 50 to halve the range (or get it right if it is 50). The guess half way between the new min and max.

Note that using the halving method for guess is the optimal method, not the most human-like, if that is what you are trying to achieve. Though it is likely that anyone with a brain would take the same approach.

Hope that helps,

Dave

##### Share on other sites
I undertsand that but the code I have written is in a do loop while

guess != thenumber;

So everytime the loop runs, it loses the last number in the guessing sequence from the memory.

So when it is between 30 and 70 say, it will guess between 1 and 70 still, not 30 and 70.

##### Share on other sites
Store the variables outside of the loop. For example:
unsigned int min( 0 );unsigned int max( 100 );unsigned int numberToGuess( 35 );bool keepTrying( true );while ( keepTrying ){ .. GuessNumber .. if number is correct then   keepTrying =false; else   if number is too low      increase min to guessed number   else     decrease max to guessed number}

Dave

##### Share on other sites
Thanks I will give it a try. I guess there are just loads of ways you can program it and my way is not ideal, I will let you know,

Cheers

##### Share on other sites
one question, the min and max number, if they are outside the loop can you access them inside it?

##### Share on other sites
You know, if you want to get anywhere coding you really need to experiment and figure things out for yourself, here is a perfect example where you could do that.

Just trying what you asked would have taken a tiny amount of time in comparison to typing and waiting for a reply to your post.

I therefore suggest you try, and once you try, if it does/doesnt work carry on reading/asking to understand why.

Basically aslong as you declare a variable in the same scope/level as the function or higher you can access it, anything in a lower scope/level you can not.

I will try and demonstrate this below...

void Function( void ){ //top level scope  bool a = true;  while( 0 )  { //second level scope     //a is acessable here as it is defined at a higher level         bool b = false;  }  //end of second level scope  // a is still accesable here as we are back to the top level scope  // b is not accessable here as it is defined at a lower scope.}

I hope this is clear.

Chris

##### Share on other sites
Like I said, Im at work at the moment otherwise I would have tried first. I am very experimental with my coding and have worked out a few things on my own.

##### Share on other sites
Quote:
 Original post by chipmeistercYou know, if you want to get anywhere coding you really need to experiment and figure things out for yourself, here is a perfect example where you could do that.

While in general I think that this is a great philosophy there are some serious pitfalls if you try doing this with C++. For example

int a[5];for(int i=1; i<=5; i++)  a[i] = 0;for(int i=1; i<=5; i++)  std::cout<<a[i]<<'\n';

This will probably compile and work as expected on most compilers. Except that it's not valid C++ code but undefined behavior. Unfortunately because it is undefined compilers will often "do the right thing" and only fail under certain difficult to reproduce conditions. This is a trivial example but undefined behavior is everywhere in C++ - one of the reasons that I discourage it as a beginners language.

##### Share on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628641
• Total Posts
2983981

• 10
• 18
• 20
• 13
• 9