I need some help on this topic: Write a program that plays a simple number-guessing with its user. The user thinks of a number and then answers a series of questions asked by the computer until it correctly guesses the number.
Think of a number in the range of 1-50 and I’ll guess it.
Output of supposed program:
Is the number 25 ? no <ENTER>
Is the number less than 25 ? yes <ENTER>
Is the number 12 ? no <ENTER>
Is the number less than 12 ? yes <ENTER>
Is the number 6 ? no <ENTER>
Is the number less than 6 ? yes <ENTER>
Is the number 9 ? no <ENTER>
Is the number less than 9 ? no <ENTER>
Is the number 10 ? yes <ENTER>
I have succeeded in guessing your number!
I need to do this for numbers in the 1-200 range.
Here is the code I have so far:
/* File: guess.c
* --------------
* This program is a guessing game.
*/
#include <cstdio>
#include "genlib.h"
#include "simpio.h"
#include "strlib.h"
int main(void)
{
int usernum,high=200,low=0,myguess=100;
string y_o_n;
printf("This program is a guessing game!\n");
while(true)
{
printf("Enter a number between 1-200. ");
usernum=GetInteger();
if(usernum>0&&usernum<201)
{
break;
}
printf("Retry.\n");
}
while(true)
{
printf("Is the number %d (yes/no)? ",myguess);
y_o_n=GetLine();
if(StringEqual(y_o_n,"no")==true)
{
printf("Is the number less than %d (yes/no)? ",myguess);
y_o_n=GetLine();
if(StringEqual(y_o_n,"yes")==true)
{
myguess=myguess/2;
}
else
{
low=myguess*2;
myguess=(myguess+high)/2;
}
}
else
{
printf("I have succeeded in guessing your number!");
getchar();
return 0;
}
}
getchar();
return 0;
}
The highlighted pieces of code are where I need help.
Right now, the program output is:
This program is a guessing game!
Enter a number between 1-200. 127
Is the number 100 (yes/no)? no
Is the number less than 100 (yes/no)? no
Is the number 150 (yes/no)? no
Is the number less than 150 (yes/no)? yes
Is the number 75 (yes/no)? no
Is the number less than 75 (yes/no)? yes
Is the number 137 (yes/no)? no
Is the number less than 137 (yes/no)? yes
Is the number 68 (yes/no)? no
Is the number less than 66 (yes/no)? yes
Is the number 134 (yes/no)? no
Is the number less than 134 (yes/no)? yes
Is the number 67 (yes/no)? no
Is the number less than 167 (yes/no)? yes
Is the number 133 (yes/no)? no
Is the number less than 133 (yes/no)? yes
Is the number 66 (yes/no)? no
Is the number less than 66 (yes/no)? yes
Is the number 133 (yes/no)? no
Is the number less than 133 (yes/no)? yes
Is the number 66 (yes/no)? no
Is the number less than 66 (yes/no)? ......
As you can see, it gets stuck at 67-132 numbers. I need help on the bold parts because that is where the next guess is calculated. Obviously, I am missing some part of the process. I need a piece of algorithm for this process. From there, I can figure out what to do (I am a pretty fast learner...).
Any advice would be great!
Thank you,
~Usandfriends
P.S.: Sorry, this code is written in C. If you want it in C++ just ask.
I didn't look into it, but it looks like there is problem with logic.
Here is how I did this:
We will create two variables: high and low. high will be a maximum possible number (200 in your case) and low will be the lowest (1 in yor case)
Then the loop starts.
guess = (low + high) / 2
If guess lower than your number: low = guess + 1
If guess higher than your number: high = guess - 1
Start again.