I'm doing the bracketing search exercise on http://www.cplusplus.com/forum/articles/12974/
I did the first star. I'm been trying to do the second star now for about 2 hours. I need help with modifying my code to make the computer guess my number. I was wondering if anyone here could help me out. This is what I got so far.
int main () {
int num; srand ( time(NULL) ); int random = rand () % 100 + 1; int tries=0; string result;
cout<<"Enter in a number for the computer to guess: ";
cin>>num;
cout<<random<<endl;
if (random!=num) cout<<"Enter if the number guessed is (Too high/Too low): ";
do{
tries++;
cin>>result;
if (random<num) {random = rand () % (100-random) + random; cout<<random<<endl; cout<<"(Too high or Too low): "; cin>> result;}
else if (random>num) {random = rand () % random; cout<<random<<endl; cout<<"(Too High or Too low): "; cin>>result;}
} while (random!=num);
cout<<"Congrats, You guessed right"<<endl;
cout<<"It took you "<<tries<<" tries";
return 0;
}
First off, please learn about indenting your code properly (using whitespace to make it readable), also next time you post code, put it in [code ]{code goes here}[/code] blocks. Doing that will make it look like so:
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <string>
usingnamespace std;
int main () {
int num;
srand ( time(NULL) );
int random = rand () % 100+1;
int tries=0;
string result;
cout<<"Enter in a number for the computer to guess: ";
cin>>num;
cout<<random<<endl;
if (random!=num) cout<<"Enter if the number guessed is (Too high/Too low): ";
do {
tries++;
cin>>result;
if (random<num)
{
random = rand () % (100-random) + random;
cout<<random<<endl;
cout<<"(Too high or Too low): ";
cin>> result;
}
elseif (random>num)
{
random = rand () % random;
cout<<random<<endl;
cout<<"(Too High or Too low): ";
cin>>result;
}
} while (random!=num);
cout<<"Congrats, You guessed right"<<endl;
cout<<"It took you "<<tries<<" tries";
return 0;
}
Now, as to your problem.
Please think about the following and get back to us with what you think the solution might be:
In the first star exercise, the computer chose the number and you had to guess, now it's the other way around. That means you won't be asking for a number as input, rather you will start the program and the computer will start guessing. Then you need to tell the computer whether it's too high or too low, and the computer will go from there.
So this basically calls for a complete reversal of your algorithm (procedure), you could almost see it as an entirely new exercise:
Design a program that guesses a number you have in your head
between 1 and 100, with every guess you need to tell the computer if he
has guessed too high or too low.
Sorry I took awhile to respond, there was something wrong with my computer.
Still need some more help with my code. This is what I got so far. It doesn't work the way I want it too. It will guess the same numbers sometimes and it will guess numbers over 100. It took 177 guesses one time.
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <string>
usingnamespace std;
int main() {
int num;
srand( (unsigned)time(NULL) );
num =rand()%100 +1;
int oldguess = num;
int result;
int tries=0;
string next;
cout<<"Think of a number and remember it. The computer will try to guess your number."<<endl<<"Enter (N) to continue"<<endl;
cin>>next;
cout<<"Is your number "<<num<<endl;
do {
tries++;
cout<<"If number is too high enter (1)"<<endl<<"If number is too low enter (2)"<<endl<<"If number is correct enter (3)"<<endl;
cin>>result;
if (result==1) {num=rand () %oldguess+0; cout<<"Is your number "<<num<<endl;}
elseif (result==2) {num=rand () %100+oldguess; cout<<"Is your number "<<num<<endl;}
} while(3!=result);
cout<<"Congrats, You guessed right"<<endl;
cout<<"It took you "<<tries<<" guesses";
}
minPossible = 1
maxPossible = 100
guess = random value in the range [minPossible,maxPossible]
numGuesses = 1
while guess is not the number chosen
{
if guess is too low
minPossible = guess+1
if guess is too high
maxPossible = guess-1
guess = rand value in the range [minPossible,maxPossible]
++numGuesses
}
output numGuesses
Glad to see you are making good progress on this, that's looking a lot better.
I just wanted to add a little bit of explanation to Cire's (excellent) pseudo code, to make sure that you understand what he means.
In order to guess the number, the computer will need to keep track of your responses (higher/lower), so that it can "zero in" on the actual number. To accomplish this, Cire's used a minPossible and maxPossible variable, which will be altered upon your answers.
Here's how that would work during your program:
1 2 3 4 5 6 7 8 9
//you think of a number: 76
computer guesses 5 -> you answer higher
//now the minPossible set is raised to 6, so the next guess will be between 6 and 100
computer guesses 56 -> you answer higher
//now the minPossible set is raised to 57, so the next guess will be between 57 and 100
computer guesses 81 -> you answer lower
//this time the maxPossible is lowered to 80, so the net guess will be between 57 and 80
//... so on until the computer can only guess it right.
I think I figured it out, but it sometimes will guess the same number. Ex: I picked the number 12. When it guess 11 I clicked 2(=too low) and it would guess 13. Then when I enter 1(=too high) it guessed 11. Then i had to enter 1 and 2 a few more times before it guessed 12. Here is my code now
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <string>
usingnamespace std;
int main() {
srand( (unsigned)time(NULL) );
int result;
int tries=0;
int min=1;
int max=100;
int num=rand()%100+1;
string next;
cout<<"Think of a number and remember it. The computer will try to guess your number."<<endl<<"Enter (N) to continue"<<endl;
cin>>next;
cout<<"Is your number "<<num<<endl;
do {
tries++;
cout<<"If number is too high enter (1)"<<endl<<"If number is too low enter (2)"<<endl<<"If number is correct enter (3)"<<endl;
cin>>result;
if (2==result) {min=num; num=rand() % (max-min +1) +min; cout<<num<<endl;}
elseif (1==result) {max=num; num=rand()% (max-min +1) +min; cout<<num<<endl;}
} while(3!=result);
cout<<"Congrats, You guessed right"<<endl;
cout<<"It took you "<<tries<<" guesses";
}
Your indentation is still not very good. You should probably adopt something like what NwN showed you in his post.
Anyway, the reason is because when you are setting your min and max variables after the user tells you whether the guess was too high or low, you set them equal instead of one higher or lower. Look closely and you should see why this isn't correct.
I figured it out. But I was trying to make the code output "Error invalid entry" if the user enter anything other than 1,2,or 3 but it messes up if I enter a letter. I tried unsigned char result but it doesn't work. my code works great as long as the user follows directions. And sorry, I forgot about the indentation advice NwN gave me. Is this any better.
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <string>
usingnamespace std;
int main() {
srand( (unsigned)time(NULL) );
int result;
int tries=0;
int min=1;
int max=100;
int num=rand()%100+1;
string next;
cout<<"Think of a number and remember it. The computer will try to guess your number."<<endl<<"Enter (N) to continue"<<endl;
cin>>next;
cout<<"Is your number "<<num<<endl;
do {
tries++;
cout<<"If number is too high enter (1)"<<endl<<"If number is too low enter (2)"<<endl<<"If number is correct enter (3)"<<endl;
cin>>result;
if (1==result)
{
max=num -1;
num=rand() % (max-min +1) + min;
cout<<"Is your number "<<num<<endl;
}
elseif (2==result)
{
min=num +1;
num=rand()% (max-min +1) + min;
cout<<"Is you number "<<num<<endl;
}
if (result>3)
{
cout<<"Error, invalid entry"<<endl;
cout<<"Is your number "<<num<<endl;
}
} while(3!=result);
cout<<"Congrats, You guessed right"<<endl;
cout<<"It took you "<<tries<<" guesses";
}