Input Validation in C++

Written by Raza on. Posted in C++

C++ contains different data types which include integer, character, float, double, arrays, etc. When we declare a variable of one data type, it can hold the data of its own type only. For example if we declare an integer type variable, we can input only an int value in it. Similar is the case with other types. But what would happen if we insist upon storing a character in an integer variable? So let us see.

girl

In C++, character type variables store data in the form of ASCII (American Standard Code for information Interchange). Every character is assigned and stored in memory using a unique numeric value. In this way, numbers could also be stored in a char type variable. But it’s not true the other way round.

Integers can hold only numeric data. If we input a character in it, the program gets stuck or goes into an infinite loop. To avoid the non-numeric data entering in an int variable, we can employ one or two small techniques using the knowledge of input/output streams.

Remember one or two small things before we proceed towards the code. Whenever an input operation is performed, compiler checks the condition of the stream. If the operation performed was perfect and legal, a flag is set up which is known as goodbit. Conversely if the input operation had experienced any error, the failbit or badbit flag is setup. You can assume that these flags are the condition or state of the stream.

With this piece of knowledge at our hand, we can write a basic source code for input validation and then I would explain it.

</p>
<p>int a ;<br />
&nbsp;</p>
<p>while (1)<br />
{<br />
&nbsp; &nbsp; cout << "Please Enter the Number" << endl ;</p>
<p>&nbsp; &nbsp; if (cin >> a)<br />
&nbsp; &nbsp; &nbsp; &nbsp; break ;<br />
&nbsp; &nbsp; else<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; cout << "Invalid Number" << endl ;<br />
&nbsp; &nbsp; &nbsp; &nbsp; cin.clear () ;<br />
&nbsp; &nbsp; &nbsp; &nbsp; //cin.ignore (numeric_limits<streamsize> ::max (), '\n') ;<br />
&nbsp; &nbsp; &nbsp; &nbsp; while (cin.get () == '\n') ;<br />
&nbsp; &nbsp; }<br />
}</p>
<p>

Basically what this code is doing is that it prompts the user to enter a number. The input is taken inside an if statement. If you are familiar with operator overloading in C++, you may know that the ‘cin >>’ call returns the reference of istream class, i.e. ‘istream&’. If the input operation has been successful, the goodbit flag would be set up and if condition would be true.

If the user enters a character in the int variable, the operation would be incorrect and the failbit or badbit flag would be setup. Thus the if condition would be false and the program would go in the else part. In else part, we have to first clear the state of all error flags. So we use cin.clear () for that purpose.

Now a stream ends when the delimiting character is encountered which is ‘\n’. So in order to start off from a fresh stream, we have to extract all the characters from the current stream and discard them. So we use the while loop for that purpose. We can also use the line commented instead of a while loop. We would have to include limits.h in the header. It ignores all the characters in the stream.

Finally instead of using cin in the if condition, you can use this method as well.

</p>
<p>int a ;</p>
<p>while (1)<br />
{</p>
<p>&nbsp; &nbsp; cout << "Please Enter the Number" << endl ;<br />
&nbsp; &nbsp; cin >> a ;<br />
&nbsp; &nbsp; if (!cin.fail ())<br />
&nbsp; &nbsp; &nbsp; &nbsp; break ;<br />
&nbsp; &nbsp; else<br />
&nbsp; &nbsp; {</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; cout << "Invalid Number" << endl ;<br />
&nbsp; &nbsp; &nbsp; &nbsp; cin.clear () ;<br />
&nbsp; &nbsp; &nbsp; &nbsp; //cin.ignore (numeric_limits<streamsize> ::max (), '\n') ;<br />
&nbsp; &nbsp; &nbsp; &nbsp; while (cin.get () == '\n') ;<br />
&nbsp; &nbsp; }<br />
}</p>
<p>

The cin.fail () checks if the failbit or goodbit has been set due to the previous input operation.

Next time I would for sure return with another Sorting Algorithm. Good Bye!

Tags: ,