# Thread: variations on guess my number

1. ## variations on guess my number

Here is a simple looping version I made of the "guess my number" program.

So I have some questions:
What are some other/better ways to write it, and how do you make the number the user must guess random for each run of the program? How would you specify that it's an integer between one and ten, for example?

In this the number you have to guess is always the same. It's boring but it works. Written in Dev-C++.

Code:
```#include <iostream>
using namespace std;

int main ()
{
int guess;
cout<<"I am thinking of an integer between 1 and 10. Try to guess what it is!\n";
cin>>guess;

while (guess!=5)
{
if (guess==1 || guess==2 || guess==9 || guess==10){cout<<"Guess again!\n";}
if (guess==3){cout<<"A little higher.\n";}
if (guess==4||guess==6){cout<<"So close!\n";}
if (guess==8){cout<<"Too high.\n";}
if (guess==7){cout<<"A little lower.\n";}
cin>>guess;
}
if (guess==5){cout<<"You got it!\n";}

system("pause");
return 0;
}```

2. Look up rand() from <cstdlib>.
The simplest way to specify the range is to use modulus.

I also suggest trying to generalize your code. You shouldn't need a test for each possible guess. Maybe try using the difference between the answer and the guess?