1

I'm a self taught developer trying to learn c++, I found this exercise on google and I wrote the code for it, though all my conditions were correct, it wouldn't work, when I checked their answer, I found this line of code- if (!(cin >> guess)). I honestly don't see the relevance, I don't know why it made my loop not to work. Here is my code:

int main(int argc, char* argv[])    
{

int nUserRandNum = 0;
    int randomNumber=0;

    srand (time(NULL));
    randomNumber = rand() % 100 + 1;

    printf("Please enter a random number between 1 - 99 \n");

    scanf("%d", &nUserRandNum);


    do
    {
        if (randomNumber < nUserRandNum)
        {
            printf("Try to go a little higher than \n", nUserRandNum);
        }
        else
        {
            printf("You might want to go a little lower than \n", nUserRandNum);    
        }
    }
    while (randomNumber != nUserRandNum);

    printf("You got it!!!");

    system("Pause"); 

    return 0;
}

When I checked the answer they had:

int random_number, guess;

// Initialize random seed.
srand (time(NULL));

// Generate random number between 1 and 100
random_number = rand() % 100 + 1;

cout << "Guess our number (1 to 100) ";
cin>>guess;
do 
{
    if (!(cin >> guess)) 
    {
      cout << "Please enter only numbers" << endl;
    } 
    else 
    {
      if (random_number < guess) 
          cout << "The secret number is lower than " << guess << endl;
      else if (random_number > guess) 
          cout << "The secret number is higher than " << guess << endl;
    }
} while (random_number != guess);
cout << "Congratulations!" <<  endl;

what does that if statement do {if (!(cin >> guess)) }? And are there other reasons my loop didn't work?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • You don't read in answers in your loop-your user gets only a single guess. – Dave Newton Apr 19 '17 at 18:05
  • 1
    `cin >> guess` is the input operation - it's reading the next input value from the standard input stream and storing it in `guess`. Since you're reading into an integer variable, the operation will fail (evaluate to false) if the input string is not a valid integer constant (contains something other than digits). If you enter something that isn't an integer, you get the `"Please enter only numbers"` message - otherwise it compares the input to the random number. – John Bode Apr 19 '17 at 18:08
  • 1
    Expansion to @JohnBode 's comment on what's happening: [Why and how it happens](http://stackoverflow.com/questions/4600295/what-is-the-meaning-of-operator-bool-const). And more details: http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool – user4581301 Apr 19 '17 at 18:10

2 Answers2

2

The difference between scanf and cin >> is not relevant here, that's not what makes it work.

Here's what you have:

scanf("%d", &nUserRandNum);
do
{
    ... print ...
}
while (randomNumber != nUserRandNum);

Your scanf is outside of the loop. Therefore, when the do...while condition is satisfied, the same number that the user already entered is checked again.

This needs to be inside the loop body, like so:

do
{
    scanf("%d", &nUserRandNum);
    ... print ...
}
while (randomNumber != nUserRandNum);

Reading input from cin rather than using scanf is probably a good idea, as it's less easy to get wrong. (Although the code you've shown still manages to do so.)

Checking whether a number was successfully read is probably a good idea too. When the user enters random garbage, you shouldn't treat that as if the user had entered a number.

But neither of those is the cause of your problem.

  • Oh my! I totally understand it now, because the scanf is not in the loop, he only gets to input once, so the loop goes on forever regardless. Bless you!

    Alright I'll do well to ensure that I verify that what was entered was a number before the loop runs. Thank you

    – HeelsPlusPlus Apr 20 '17 at 08:17
1

You can understand that if (!(cin >> guess)) statement as "if inputting the number was unsuccessful", so (cin >> x) is true if the type entered by the user really is an int. Nitpick: you switched "go lower" with "go higher" by the way.