Input Validation of an int

I am trying to validate the length entered by a user. However, as I enter random values when testing the code below it continues to my next statement if I enter 1asdsa for the value of length.

cout << "\nRectangular Pool" << endl << endl;
cout << "Please input the length of the desired pool (rounding up to the nearest half foot, ex: 11.5): ";
cin >> length;

// Legth Input Validation
while (length < 1 || cin.fail())
{
cin.clear();
cin.ignore(INT_MAX, '\n');
cout << "\nYour have enter an invalid leght! Please try again! " << endl;
cout << "Please input the length of the desired pool (rounding up to the nearest half foot, ex: 11.5): ";
cin >> length;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cctype>
#include <cmath>

double get_length()
{
    std::cout << "length of the desired pool (a value not less than 1): " ;

    double length ;

    if( std::cin >> length ) // if the user entered a number
    {
        // find out (peek) what the next input character is
        const auto next = std::cin.peek() ;

        // if it is not a white space, the input is invalid eg. 1asdsa
        if( !std::isspace( static_cast<unsigned int>(next) ) )
            std::cout << "invalid input, a bad character immediately after the number" ;

        // if the length entered is less than one, the input is invalid
        else if( length < 1.0 ) std::cout << "invalid input, the length can't be less than one" ;

        else // if we reach here, we got a positive length
             return std::round(length*2) / 2.0 ; // round length to the nearest half-foot and return it
    }

    else std::cout << "invalid input, not a number" ; // a non-number was entered

    // if we reach here, the input was invalid (return was not executed)
    std::cin.clear() ; // clear a possible failed state of the stream
    std::cin.ignore( 1'000, '\n' ) ; // throw the rest of the bad input line away
    std::cout << ". try again.\n" ;
    return get_length() ; // try again
}

int main()
{
    const auto len = get_length() ;
    std::cout << len << '\n' ;
} 
The basic stream extractors just stop processing the stream at the first character which doesn't match the expected conversion - in this case an int.

If you want to validate a whole line of input, you need to read it into a string first, then validate that string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
    string line;
    cout << "say it > ";
    if ( getline(cin,line) ) {
        istringstream is(line);
        int answer;
        // it's an integer, and ONLY an integer
        if ( is >> answer && is.eof() ) {
            cout << "You typed " << answer << "\n";  
        } else {
            cout << "Bad input\n";
        }
    } else {
        cout << "Ok, bye then\n";
    }
    return 0;
}

Topic archived. No new replies allowed.