1. ## Random number generation

I am trying to write a piece of code using a random number generation result. I have not been able to successfully debug (see below). Could you assist? I am very new to this programming language.

// Guess - Number Game
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <cstdlib>

int main()
{

int guessCounter = 1,
x;

for (int guess = 1; guess <=20; guess++) {
x = 1 + rand() % 1;

cout <<"I've got the number. Please guess it:";
cin >> guess;

if (guess < x)
cout << "It's less than the number. Please guess again:";

if (guess > x)
cout << "It's greater than the number. Please guess again:";
else
break;
guessCounter = guessCounter+ 1;
cout<<"Input next guess.";

if (guess == x)
cout << "You guessed it!"<<endl;

}
return 0;

}

2. ## WHOA!

You made this way too complex. First off, it doesnt do it quite corectly. I runs it 20 times regardless if the guess it right or not. Here, I wil hep you by giving you some usefull code. It may not all be correct in the random part, because i am not changing that. What compiler are you using?

Code:
```#include <iostream>
#include <cstdlib>

using std::cout;
using std::cin;
using std::endl;

int main()
{

int guessCounter = 0;
int x = 1 + rand() % 1;

cout <<"I've got the number. Please guess it:";

while (guess != x)
{
cin >> guess;

if (guess < x)
cout << "It's less than the number. Please guess again:";

if (guess > x)
cout << "It's greater than the number. Please guess again:";

if (guess == x)
cout << "You guessed it!"<<endl;

guessCounter = guessCounter+ 1;

cout<<"Input next guess.";
}
return 0;
}```

3. Code:
```// Guess - Number Game
#include <iostream>
#include <cstdlib>
#include <ctime>

using std::cout;
using std::cin;
using std::endl;

int main()
{
int guessCounter, x, guess;

// Only get a single random number, otherwise you are getting
// a new one each time you go through the loop.  Also, you need
// to seed the random number generator otherwise you get the
// same "random" number each time you run the program.  Last,
// what you had will only give 1 as the number, try picking something
// larger to do the "%" with, for example, this will give a number
// from 1 to 100.

srand( time(NULL) );
x = 1 + rand() % 100;
cout <<"I've got the number. Please guess it:";

for( guessCounter = 0; guessCounter < 20; guessCounter++ )
{
cin >> guess;

if (guess < x)
cout << "Your guess is less than the number. Please guess again:";

else if (guess > x)
cout << "Your guess is greater than the number. Please guess again:";
else if (guess == x)
{
cout << "You guessed it!"<<endl;
break;
}
}

return 0;

}```

4. Try this:

PHP Code:
``` #include <iostream>  #include <cstdlib>  #include <ctime> using namespace std; int main()  {           int guessCounter = 1,      int x = 0;      int guess = 0;     srand( time(NULL) );     x = 1 + rand() % 1; // will give you a random number from 1 to 2 (?)          cout <<"I've got the number. Please guess it:";           for( guessCounter = 0; guessCounter < 20; guessCounter++ )     {          cin >> guess;          if (guess < x)          {             cout << "It's less than the number. Please guess again:";          }         else if (guess > x)          {             cout << "It's greater than the number. Please guess again:";          }         else         {             // if it's neither < nor >, it should be ==             break;         }                     cout<<"Input next guess.";      }     if (guess == x)      {         cout << "You guessed it!"<<endl;      }     else     {         cout << "You lost. It was " << x << endl;     }           return 0;  }  ```

5. yeah, what nvoigt put would work, but you'll only get 19 guesses with that, lol. he meant '<=20' i think. or you can do this:

Code:
```#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
int guesses=1;
int guess=0;
int x=0;

srand(TIME(NULL));

x=1 + rand() % (5-1+1); //in between 1 and 5

do
{
cin>>guess;

if(guess>x)
cout<<"Too high. Guess again."<<endl;
if(guess<x)
cout<<"Too low. Guess again."<<endl;
if(guess==x)
break;

guesses++
}while(guesses<=20);

if(guesses==21)
cout<<"You lost"<<endl;
if(guesses<21)
cout<<"You won"<<endl;

return 0;
}```