Hello, I need help creating a program where the user is asked to think a number between 1 and 10, and the computer tries to (randomly) “guess” the number. The user should provide clues to the computer so that the random number the computer generates keeps getting closer to the answer in each step. The following is a sample run of the program:
Think of a number between 1 and 10. Is 3 the number you are thinking of? Enter h if the number you are thinking of is higher, l if it is lower or c if it is correct.
h
Is 5 the number you are thinking of? Enter h if the number you are thinking of is higher, l if it is lower or c if it is correct.
h
Is 9 the number you are thinking of? Enter h if the number you are thinking of is higher, l if it is lower or c if it is correct.
c
I guessed your number in 3 steps.
I really have no idea where to start. Any help will be greatly appreciated. Thanks
Well, to start with, you'll need to generate a random number. To do this, you'll first need to seed the random number generator with something variable - this is usually done with the current time. Then, to constrain the results, we can do a modulus of our desired maximum, plus the desired minimum. So, to put it all together:
1 2 3 4 5 6 7 8
#include <cstdlib> //needed for rand() and srand()
#include <ctime> //needed for time()
int main() {
int max = 10, min = 1;
srand(time(0)); //seed the random number generator
int r = (rand() % max) + min;
}
From there, it should just be a case of changing you're min/max constraints based on the user's input.
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <conio.h>
int main()
{
randomize();
int num,random_num,numd,lowerlimit=0,upperlimit,no_of_times=0; char choice='a';
cout<<"\n enter num 0-10 ";
cin>>num;
upperlimit=num+1;
while(choice!='c')
{
random_num=random(upperlimit-lowerlimit+1)+lowerlimit;
cout<<"\n is your no h/l/c than "<< random_num <<" ? ";
cin>>choice;
switch(choice)
{
case'h' : lowerlimit=random_num;
break;
case'l' : upperlimit=random_num;
break;
default : ;
}
no_of_times++;
}
cout<<"\n your no. is "<<num<<" and i found it in "<<no_of_times<<" steps \n";
system("pause");
return 0;
}
#include <iostream>
usingnamespace std;
int main()
{
int lower,higher,maxnr,answer,myguess;
cout<<"Think of a number between 1 and ";cin>>maxnr;
higher=maxnr+1;lower=1;
while(true){
myguess=(higher+lower)/2;
cout<<"I think it is "<<myguess<<'\n';
cout<<"Is this the number ?\n";
cout<<"1.Lower\n";
cout<<"2.This is it !\n";
cout<<"3.Higher\n";
cin>>answer;
if(answer==1) higher=myguess;
if(answer==2) {cout<<"It was fun wasn't it\n"; return 0;}
if(answer==3) lower=myguess;}
return 0;
}
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
void pause()
{
//Prevents the application from exiting imediatly ...
cout<<endl<<"Press any key to continue . . .";
cin.ignore();
cin.get();
}
int main()
{
int maxnumber,minnumber=1,answer,number,aux;
srand(time(0));
cout<<"Think of a number between 1 and ";cin>>maxnumber;
++maxnumber;
while(answer!=2){
aux=maxnumber-minnumber;
if(aux){
number=rand()%aux+minnumber;}
else {cout<<"You cheated !\n"; pause(); return 0;}
cout<<"I think it is "<<number<<endl;
cout<<"It is this the number ?\n\n";
cout<<"1.Less\n";
cout<<"2.This is it!\n";
cout<<"3.Higher\n\n";
cin>>answer;cout<<endl;
if(answer==1) {
maxnumber=number;
}
if(answer==3){
minnumber=number+1;
}
if(maxnumber-1==minnumber) {cout<<"I found your number.It is "<<minnumber<<endl; pause(); return 0;}
}
pause();
return 0;
}
@aalok
Don't use conio.h, its non-standard and can cause problems with most compilers (almost everything used nowadays except VC++). Also, this is a C++ program, so use the C++ headers, not the C ones (i.e. cstdlib and ctime).
@OP
Don't seed the random number generator in a loop. Just seed it once, normally at the starto of your main function. If you are using characters, prefer to use char, unless you are using getchar() (which you shouldn't be). Also, your while loop is only infinitely looping over whether the number is '7', and then increasing the number of steps, creating an infinite loop. Consider adding {braces} around the whole area meant to be covered by the loop.
NT3 conio.h is pretty standard on windows, but the thing is that he didnt needed to use it anyawy! He didint used any conio.h function on his program.He could have used _getch() instead of the crap system ("pause"), but he didnt ! Also he used borland specific macros such as randomize().Oh and he forgot to put using namespace std; after #include <iostream>
No. It starts the program, but it doesn't try guessing the number. The program just shuts down. I use Dev- C++, I don't know if that has something to do with it.
@SorinAlex,
I used Borland's for making this code coz my Code::Blocks got hanged, so I used randomize() and all that.
And I don't think Borland's is for C++ hence, no need of usingnamespace std;
@mora14,
Try my code in a C compiler if you have one, and I'll upload a C++ code soon.
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <conio.h>
usingnamespace std;
int main()
{
int num,random_num,numd,lowerlimit=0,upperlimit,no_of_times=0; char choice='a';
cout<<"\n enter num 0-10 ";
cin>>num;
upperlimit=num+1;
while(choice!='c')
{
random_num=rand()%(upperlimit-lowerlimit+1)+lowerlimit;
cout<<"\n is your no h/l/c than "<< random_num <<" ? ";
cin>>choice;
switch(choice)
{
case'h' : lowerlimit=random_num;
break;
case'l' : upperlimit=random_num;
break;
default : ;
}
no_of_times++;
}
cout<<"\n your no. is "<<num<<" and i found it in "<<no_of_times<<" steps \n";
return 0;
}
@mora14 I don't know i use Code::Blocks.But when you start the the program it should say something like "Think of a number between 1 and " and then you have to write number ,for example 954.Then you think of a number the program will try to guess it ranging from 1 to 954(the number you entered)
Then it should say something like :
I think it is 54 (for example)
It is this the number ?
1.Less
2.This is it!
3.Higher
and then you write 1,2 or 3.The program then will narrow the range which it guesses the number and guess another number.
----------------------------------------------
For example:
Think of a number between 1 and (enter a number here)
// I enter 20
//and I think at 13
//The program will now generate a random number between 1 and 20.
I think the number is 17.
Is this the number ?
1.Less
2.This is it!
3.Higher
//I enter 1 since the number i though of is 13
//The program will now generate a random number between 1 and 16
I think the number is 3.
Is this the number ?
1.Less
2.This is it!
3.Higher
//I enter 3 since i though of is 13
//The program now generates a number between 4 and 16
I think the number is 5.
Is this the number ?
1.Less
2.This is it!
3.Higher
//I enter 3
//The program generates number between 6 and 16
I think the number is 12.
Is this the number ?
1.Less
2.This is it!
3.Higher
//I enter 3
//The program generates number between 13 and 16
I think the number is 13.
Is this the number ?
1.Less
2.This is it!
3.Higher
It works, but not exactly right. If I type 10 first( so it can generate a number between 1 & 10) it does it correctly, but when it finally guesses my number correctly, it shows it wrong. For example, I choose 5. It says, "your no. is 10 and i found it in 5 steps", instead of saying, "your no. is 5 and I found it in x steps". This gives me a good idea to work off of though. Thanks!