Hi guys!
I'm reading jumping into c++ to learn this language. I'm now in chapter 9 where it asks: "Write a program that solves the guessing game from part 1. How many guesses does your program need?"
I've already done it the easy way. Here is the code:
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int random();
int main()
{
srand(time(NULL));
int numbertoguess=random();
int tries=0;
int retry=0;
do
{
retry++;
tries=random();
if (tries==numbertoguess)
{
cout<<"the result is " <<numbertoguess<< "\n";
cout<<"the number of tries is " <<retry<< "\n";
break;
}
} while (true);
return 0;
}
int random()
{
return rand()%100+1;
}
The thing is I wanted to take it to the next level. I wanted to make the program think like a human. I wanted to change the interval with every try to make it smaller and smaller (like you would if you played this game). For example, if the number we need to find is 57 and the program tries 40, I want to change the random interval to 40-100. If the second try is 70, I want the interval to become 40-70. I don't know how I can keep one of the values and change the other. I came with this code, I just need to replace the unknown.
Keep in mind that I'm a beginner, so if it's really complicated tell me what I need to learn to achieve this! If you need more details to help me don't hesitate to tell me!
Thank you very much!
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int random(int low,int high);
int main()
{
srand(time(NULL));
int numbertoguess=random(1,100);
int tries=random(1,100);
int retry=0;
do
{
retry++;
if (tries==numbertoguess)
{
cout<<"the result is " <<numbertoguess<< "\n";
cout<<"the number of tries is " <<retry<< "\n";
break;
}
elseif (tries<numbertoguess)
{
tries=random(tries,unknown)
}
elseif (tries>numbertoguess)
{
tries=random(unknown,tries)
}
} while (true);
return 0;
}
int random(int low,int high)
{
return rand()%(high-low)+low;
}
That's what I'm trying to do. I want the computer to make the distance smaller every time he guesses a number. For example (using the numbers you gave in your reply) if 50 is too high, I want the distance to change from 1 to 50. After if 25 is too low, I want to change from 25 to 50. I just don't know how to change one of the values and keep the other. I think I just need to replace the "unknown" in my code.
Here is the source code for what you asked - I am new to c++ also but not to programming. I learn best from example myself. If you have any questions concerning this code I will be happy to help with real answers. This is how I believe a beginner would accomplish this task. If anyone wants to point out beginner style options or alternative solutions to mine please feel free. There is always more then one way to skin a cat.
@iamk2 Thank you very much! Your code is very simple! I can't understand why I didn't think of a simple solution like that! Thank you so much for helping me!
@rechard3 I'm checking the link you gave me right now!