Give a warning for invalid input in C++

In this tutorial, we are going to learn how to give a warning when the user enters invalid input.

In C++ while writing programs we use std::cin to ask the user to enter the input. The process that involves whether the input which the user has entered conforms to what the program is expecting is known as input validation. To make our program more robust validating the input becomes important.

Two ways of input validation in C++

There are two ways of validating the input and giving the warning to the user. These are as follows-

  1. Inline- Restricting the user from entering any invalid input.
  2. Post Entry- In this validation, the user enters the input then the validation is done post user entry of input.

Consider a situation where we have to accept only integer inputs from the user and reject the rest. Below is the program to illustrate the same.

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

int main(){
    int x;
    cout<<"Enter an integer"<<endl;
    cin>>x;
    
    //using infinite loop for input validation
    while(1)
    {
        if(cin.fail())//returns true when input fails
        {
            cin.clear();//use to clear error state of buffer
            
            //ignores rest of the line
            //after first error occurs
            
            //skips to the next line
            cin.ignore(numeric_limits<streamsize>::max(),'\n');
            cout<<"Please enter the valid input"<<endl;
            cin>>x;
            
        }
        if(!cin.fail())
        break;
        
    }
    //displaying the correct number
    cout<<"the number entered is: "<<x<<endl;
    return 0;
    
}

Output:

Enter an integer

sadfdf

Please enter the valid input

fdsf332

Please enter the valid input

#@R3ffd#3

Please enter the valid input

4532

the number entered is: 4532

Explanation of the following code-

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

int main(){
    int x;
    cout<<"Enter an integer"<<endl;
    cin>>x;

In this part of the code we are importing the header files and taking the input from the user.

//using infinite loop for input validation
   while(1)
   {
       if(cin.fail())//returns true when input fails
       {
           cin.clear();//use to clear error state of buffer
           
           //ignores rest of the line
           //after first error occurs
           
           //skips to the next line
           cin.ignore(numeric_limits<streamsize>::max(),'\n');
           cout<<"Please enter the valid input"<<endl;
           cin>>x;
           
       }

In this code snippet, we are running the infinite while loop for checking the correct input. We are using following functions for the successful execution of the code-

  • cin.fail()- When input failure occurs this function returns true and failure is the case when the entered input is not an integer.
  • cin.clear()- This function is used so that further processing of input can take place. This function clears the error state of the buffer( a piece of memory used for storing data temporarily while data is being exchanged) and makes sure that the wrong input warning does not get displayed an infinite number of times when the user has entered the wrong input.
  • cin.ignore()- This function is used to ignore the lines when the first instance of error occurs and skip to the next line.

After this we are prompting the user to enter the correct input whenever the input entered is incorrect.

if(!cin.fail())
        break;
        
    }
    //displaying the correct number
    cout<<"the number entered is: "<<x<<endl;
    return 0;
    
}

If the input is correct and the fail() function is false we break from the loop and display the correct output.

Thus, we have learned how to give a warning sign to the user in C++ using various inbuilt functions.

Leave a Reply

Your email address will not be published.