how do you prevent the dowhile loop from breaking?

$ clang++-7 -Wall -o main main.cpp
main.cpp:23:39: warning: variable 'humanInputvalidator' is
      uninitialized when used here [-Wuninitialized]
        for (int humanInputvalidator; humanInputvalidator < hum...
                                      ^~~~~~~~~~~~~~~~~~~
main.cpp:23:37: note: initialize the variable 'humanInputvalidator' to
      silence this warning
        for (int humanInputvalidator; humanInputvalidator < hum...
                                    ^
                                     = 0
1 warning generated.

So let’s fix that.

Additionally, on the second iteration of getting input from the user, humanInputvalidator < humanInput.length() && isHuman will always be false. You’ll want to reinitialize isHuman = true inside the do while loop.

Functions are a great way to simplify your code!

Use functions! It’s a great way to simplify your code and decrease the number of variables in any single context.

bool isThisHumanInput(std::string data) {
  for (int i = 0; i < data.length(); i++)
    if (!(data[i] >= 48 && data[i] <= 57))
      return false;
  return true;
}

void botVarification()
{
    string humanInput;
    bool isHuman;
    do {
      cout << "Enter the number to verify that you are not a bot: ";
      cin >> humanInput;
      isHuman = isThisHumanInput(humanInput);
      if (isHuman) {
        cout << "Success" << '\n';
      } else {
        cout << "Not success, try again!\n";
      }
    } while (!isHuman);
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top