0

In my c++ code, I would like to validate my user input to be an int between 1,10 using a do while loop. I am able to validated for integers outside of the range. However if user inputs a float or a letter, it becomes an infinite loop. My idea is to add a condition in my while loop for if the input is not an integer to keep asking for input. the CAPITAL letters is where I am having trouble.

#include <iostream>
using namespace std;


int main(){
    cout << "Welcome, ";
    int steps;
    int count=0;
    do{
        cout << "How many? \n";
        cin >> steps;
        IF (STEPS IS NOT INTEGER==TRUE){
            COUNT=1;
        }
        if (steps <1)
        {
            cout << "not enough...\n";
        }
        if (steps > 10){
            cout << "too many steps.\n Please pick a lower number of steps.\n\n";
        } 
      } while (steps < 1|| steps >10 || COUNT==1);
    
    //doing stuff with valid input


    return 0;
}

Essentially I am trying to add another condition that just returns a boolean. and if the boolean implies that the input is not valid, then it reassigns count to make sure the do while loops continues until the input is valid.

The problem i am working on asks for a max and min steps, since all of them were having a similar problem i tried to simplify it and forgot some of the edits.

7
  • 3
    You declared that steps is an integer, it can't be anything other then that. If you want to make sure the user inputs a number into the console, then you need to use strings and parse it to see if it is in the expected format. Commented Jul 27, 2020 at 18:36
  • 1
    stackoverflow.com/questions/1283302/… Check out the top answer for this question. You don't necessarily need to do parsing to make sure you have an integer. Commented Jul 27, 2020 at 18:37
  • cin >> steps; is reading into an integer variable. By definition, the result will be an int.. Commented Jul 27, 2020 at 18:38
  • 4
    @MPops The problem with solutions like that is that they do not keep the stream clean. If I enter 1a, 1 will get stored in steps and the a would be left in the stream to mess with the next input. Commented Jul 27, 2020 at 18:54
  • Think that as steps is declared int, it will never receive a non-int value ! Commented Jul 27, 2020 at 19:29

2 Answers 2

2

You can check whether the input failed, i.e. the user entered something that could not be read as an int like this:

if (cin.fail()) {  // in place of IF (STEPS IS NOT INTEGER==TRUE)
  cin.clear();      
  cin.ignore();
  cout << "not an integer, try again\n";
  continue;
}

This avoids the need for the COUNT variable.

Also, your while condition doesn't appear to match the checks inside the loop. What happens when step is either 9 or 10? You should be consistent with the checks inside the loop.

Sign up to request clarification or add additional context in comments.

16 Comments

you also need to ingnore the left over user input, no? I have to admit I never can remember the "right way"
cin.ignore(); here can be trouble. For example A3A will fail. The A will be ignored, and the next try will find the 3 and accept it. If this is the behaviour you want, cool beans. If not, you probably want to read into a std::string to get the whole A3A token out of the stream.
There's no authentic alternative to reading into a string. The problem is that it's beyond many newbies to write the code that checks the string and then converts.
@MPops depends on the input requirements. Ignoring to linefeed may not help if there are multiple tokens on the line.
@user4581301 Ah, so if you have a comma separated or really anything more complex than just a new-line separated input, it's better to load the whole thing and proceed
|
0

You could use the ! operator.

For example:

if ( !(std::cin >> steps) )
        {
          std::cin.clear();
          std::cin.ignore();
          std::cout << "Incorrect entry. Try again: ";
        }

Also consider not using using namespace std;.

2 Comments

why should i not use using namespace std;
click on it, there is a link to that exact question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.