Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good input validation loop using cin - C++

Tags:

I'm in my second OOP class, and my first class was taught in C#, so I'm new to C++ and currently I am practicing input validation using cin. So here's my question:

Is this loop I constructed a pretty good way of validating input? Or is there a more common/accepted way of doing it?

Thanks!

Code:

int taxableIncome;
int error;

// input validation loop
do
{
    error = 0;
    cout << "Please enter in your taxable income: ";
    cin >> taxableIncome;
    if (cin.fail())
    {
        cout << "Please enter a valid integer" << endl;
        error = 1;
        cin.clear();
        cin.ignore(80, '\n');
    }
}while(error == 1);
like image 686
Alex Avatar asked Jan 16 '10 01:01

Alex


People also ask

What type of loop is best for validating input?

Often it is necessary to validate data input by the user, and repeat the request for the data in the case where the input of the user is not valid. This can be done by using a do loop.

How do you write an input validation loop?

To validate input in a while loop: Use a try/except or an if/else statement to validate the input. If the input is invalid, use a continue statement to continue to the next iteration. If the input is valid, use a break statement to break out of the loop.

Which is best practice in input validation?

Just in time validations One of the best practices in form validation is to inform your users when they make an error so they can immediately verify and correct it before they take the next step. This way, you avoid error messages in the input field but also helps users build their confidence in what they are doing.

What is proper input validation?

Input validation (also known as data validation) is the proper testing of any input supplied by a user or application. Input validation prevents improperly formed data from entering an information system. Input validation can be carried out on client-side and server-side code to reinforce infrastructure security.


1 Answers

I'm not a huge fan of turning on exceptions for iostreams. I/O errors aren't exceptional enough, in that errors are often very likely. I prefer only to use exceptions for less frequent error conditions.

The code isn't bad, but skipping 80 characters is a bit arbitrary, and the error variable isn't necessary if you fiddle with the loop (and should be bool if you keep it). You can put the read from cin directly into an if, which is perhaps more of a Perl idiom.

Here's my take:

int taxableIncome;

for (;;) {
    cout << "Please enter in your taxable income: ";
    if (cin >> taxableIncome) {
        break;
    } else {
        cout << "Please enter a valid integer" << endl;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
}

Apart from only skipping 80 characters, these are only minor quibbles, and are more a matter of preferred style.

like image 158
P-Nuts Avatar answered Oct 24 '22 02:10

P-Nuts