8
Validating user input in C++
Validating
C++
Input

Inputs have to be validated before allowing any kind of processing or operations to be performed on it. This is extremely important because , an unhandled wrong input might have the complete ability to crash a system. C++ has some good validation techniques that can be used to validate most kind of inputs. This post discusses some of the techniques and its shortcomings and what could be done to improve the quality of validation.

Now, consider a program has to accept only integer inputs and reject all the others. So, the developer would have declared to store the integer value in say “int a;”. So “a” will store the input value.

When the user input is accepted using the “cin>>a” statement, we can use the inbuilt methods surrounding the “cin” statement to test its status.

Here is a sample program: -

    #include<iostream>
#include<limits>
using namespace std;

int main()
{
int a;

cout<<“Enter an integer number\n”;
cin>>a;
while(1)
{
if(cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),’\n’);
cout<<“You have entered wrong input”<<endl;
cin>>a;
}
if(!cin.fail())
break;
}

cout<<“the number is: “<<a<<endl;
return 0;
}


From the above example, the various functions are used to validate the input like the cin.fail(), cin.ignore(), etc. The various functions of these methods are :

cin.fail() - This function returns true when an input failure occurs. In this case it would be an input that is not an integer. If the cin fails then the input buffer is kept in an error state. cin.clear() - This is used to clear the error state of the buffer so that further processing of input can take place. This ensures that the input does not lead to an infinite loop of error message display. cin.ignore() - This function is used to ignore the rest of the line after the first instance of error that has occurred and it skips to or moves to the next line. cin.eof() - This function can be used to check end of file errors. This returns 1 if the program tried reading something but , it was from the end of the file.

Here is a screenshot of the various wrong inputs given, and them being handled by the program till a correct input is provided.

Though this technique seems to work fine it hides a dangerous fault that can occur. This can be considered as a dis advantage of using c++. This technique does not catch certain types of input. The following screenshots show 2 inputs that is accepted and the numerical part is printed.

So, how can this erroneous inputs be handled? The technique we can apply is to accept the input as a string. The analyse the string to be of the illegal types shown above using regular expressions. If the input is valid then convert it into an integer and use it in the program else display an error message. Though its possible, its very difficult to be achieved in c++. This is mainly because , c++ doesn’t support regular expressions by default, we have to make use of the regex library which is quite complex to use. So to conclude, though C++ provides some handy tools to validate inputs, it does not help us to cover all possible situations in a straight forward way, which would lead us to use other workarounds to get our job done. But the validation of input can be handled in an efficient way with languages like java and c#.

Author