1

I want to obtain an input from the user and validate it as an integer, however my method seems to fail, when a letter or word the program just ends. Any one have a quick and easy fix for this?

int getUserInput(){

    int maxNumber;

    // the user input a valid integer, process it

    if (cin >> maxNumber)
    {
        if (maxNumber>1 || maxNumber<=100) {
            return maxNumber;
        }
    } else{
        getUserInput();
    }

}
Unknownzdx
  • 179
  • 2
  • 14
  • 1
    `getUserInput();` doesn't "goto" the start of the function. It starts a whole other call to the function. The current function call will eventually have to finish when this new call is done, and you will eventually reach the end of the function call without returning anything. – François Andrieux Feb 13 '19 at 20:03
  • 1
    You are missing the `return` in the recursive call. Need to use `retrun getUserInput();`. – R Sahu Feb 13 '19 at 20:04
  • 1
    Use a `do-while` loop, not recursion. If someone had the patience, they could blow out the stack if they entered `0` many times. – PaulMcKenzie Feb 13 '19 at 20:05
  • Also note that `std::cin` will be put into a failed state if you try to read an `int` when the input is not readable as an `int`. You will need to clear the input and reset the fail bits. – François Andrieux Feb 13 '19 at 20:05
  • Also, is there a maximum number of tries to get the input correct? What if the user wants to say "I quit"? Or is the user forced to keep trying forever? – PaulMcKenzie Feb 13 '19 at 20:11
  • `maxNumber>1 || maxNumber<=100` seems wrong. It will let you have any number at all. Did you mean `&&` rather than `||`? –  Feb 13 '19 at 20:36

2 Answers2

1

If the user does not type in an integer, your code enters an infinite recursive loop that it does not recover from, eventually overflowing the call stack.

Try something more like this instead:

int getUserInput()
{    
    int number;

    cout << "Enter a number between 1 - 100: ";

    do
    {
        // if the user input a valid integer, process it

        if (cin >> number)
        {
            if (number >= 1 && number <= 100)
                break;

            cout << "Number out of range, try again: ";
        }
        else
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');

            cout << "Invalid input, try again: ";
        }
    }
    while (true);

    return number;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I found the do-while loop hard to see where or if it terminates. – Bo R Feb 13 '19 at 21:33
  • It terminates on the `break`, which is reaches only if the user enters a valid integer in the acceptable range – Remy Lebeau Feb 13 '19 at 21:48
  • Yes, I saw that (later) but not after missing the `break` the first time around, which was my point. – Bo R Feb 13 '19 at 21:50
  • Obviously we have different coding styles. I find your answer quite ugly to read, even if it is functional. I prefer to use simpler/cleaner statements, makes debugging easier. – Remy Lebeau Feb 13 '19 at 21:53
1

You didn't state what range you wanted the input to be in so adjust accordingly.

int getUserInput() {

    int maxNumber;

    while (!(cin >> maxNumber) || maxNumber <= 1 || 100 < maxNumber) {
        if (cin)
            cout << "number out of range, try again: ";
        else {
            cin.clear();
            cin.ignore(numeric_limits<int>::max(), '\n');
            cout << "Not a number, please try again : ";
        }
    }

    return maxNumber;
}
Bo R
  • 2,334
  • 1
  • 9
  • 17