wangowango
wangowango

Reputation: 43

How to get a while loop to work with a number generator?

So for my exercise, I have to generate a random number between 1 and 100 using the srand function. And I got that part to work, but then I have to add a loop so that users can guess again if they didn't get it right on the first try. I can't seem to make the while loop work.

I'm writing the code on text edit and using Xcode as a compiler through the terminal app on mac. And I'm doing my best using the programming terminology--I'm new so if anything sounds off, that's why.

int main()
{
  srand(time(0));
  int randomNumber = 1 + (rand() % 100);
  int humanGuess;


  while (true)
  {
    cout << "Guess a number between 1 and 100!" << endl; 
    cin >> humanGuess;
    cin.ignore(1000, 10);

    if (humanGuess == randomNumber) break; 
    cout << "Great job. You got it! It's " << randomNumber << endl; 

    if (humanGuess >= randomNumber)
    cout << "Too high. Try again!" << endl;

    if (humanGuess <= randomNumber)
    cout << "Too low. Try again!" << endl;
  }//while 
}

There is no error message, but it's not compiling right. This is what I keep getting when I run it on Terminal:

Guess a number between 1 and 100!
23
Great job. You got it! It's 55
Too low. Try again!
Guess a number between 1 and 100!
78
Great job. You got it! It's 55
Too high. Try again!
Guess a number between 1 and 100!

Upvotes: 0

Views: 68

Answers (2)

Owl
Owl

Reputation: 1562

You are breaking too early. Try moving the break after the print out. Also it costs nothing in performance to initialise numbers to a known state, it's good practice to, not because there's anything wrong with this code, but if there is additional code inserted that relies on the humanGuess variable - say, then this may not always be the same value.

int main(){
  srand(time(0));
  int randomNumber = 1 + (rand() % 100);
  int humanGuess=0;

  while (true){

    cout << "Guess a number between 1 and 100!" << endl; 
    cin >> humanGuess;
    cin.ignore(1000, 10);

    if (humanGuess == randomNumber){  
       cout << "Great job. You got it! It's " << randomNumber << endl; 
       break;
    }    

    if (humanGuess >= randomNumber)
    cout << "Too high. Try again!" << endl;

    if (humanGuess <= randomNumber)
    cout << "Too low. Try again!" << endl;
  }//while 
}

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409216

Your code, as shown in the question is (with the break on its own line)

if (humanGuess == randomNumber)
    break;

cout << "Great job. You got it! It's " << randomNumber << endl; 

If the condition humanGuess == randomNumber is true, you break out of the loop with the break statement. Otherwise you unconditionally print "Great job...".

You need to put the printing as a part of the body of the if statement, and break after you print:

if (humanGuess == randomNumber)
{
    cout << "Great job. You got it! It's " << randomNumber << endl; 
    break;
}

Upvotes: 1

Related Questions