In this post, we will see about Input validation in C++.
Table of Contents
How to check user input in C++?
To stop the user from entering wrong data in the field, we can use input validation methods in C++. It is a process for checking if the data entered by a user meets some criteria or not. This process can also be broken mainly into two types: numeric and string.
String Input Validation In C++
With this technique, we can accept all user input as a string, and then we can reject or accept the string as per the requirements. Example: When we ask a user to enter a phone number, then we want to ensure that the user enters only ten digits. To implement this in C++, it has a regular expression library which can make input validation in C + + a little easier.
In C++, there are numerous functions which can help in determining if the characters entered are numbers or letters. The functions mentioned below can be used with the ‘cctype’ header.
Further reading:
Functions:
| Function | Description |
|---|---|
isalnum(int) |
It will return non-zero if the parameter passed is a digit or a letter. |
isalpha(int) |
It will return non-zero if the parameter passed is a letter. |
iscntrl(int) |
It will return non-zero if the parameter passed is a control character. |
isdigit(int) |
It will return non-zero if the parameter passed is a digit. |
isgraph(int) |
It will return non-zero if the parameter passed is a printable character that is not whitespace. |
isprint(int) |
It will return non-zero if the parameter passed is printable character (Including the whitespaces). |
ispunct(int) |
It will return non-zero if the parameter passed is either alphanumeric or whitespace. |
isspace(int) |
It will return non-zero if the parameter passed is whitespace. |
isxdigit(int) |
It will return non-zero if the parameter passed is a hexadecimal digit(0-9,A-F,a-f). |
Code:
|
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 |
//PROGRAM TO CHECK IF THE NAME ENTERED CONTAINS ONLY CHARACTERS #include <algorithm> #include <cctype> #include <iostream> #include <string> #include <string_view> using namespace std; //We will implement input validation with custom defined function //We will string_view to pass the string inside the function //string_view cannot modify the string. bool isValidName(string_view name) { return all_of(name.begin(), name.end(), [](char ch) { return (isalpha(ch) || isspace(ch)); }); } int main() { string name=""; cout << "Enter the name of the person: "; getline(cin,name); if(isValidName(name)){ cout<<"The name entered is "<< name; } else{ cout<<"Enter a Valid Name!!"; } return 0; } |
Input:
Enter the name of the person: Java2Blog
Enter the name of the person: Test123
Output:
Enter a Valid Name!!
Enter a Valid Name!!
Note:
all_offunction will return true ifpredreturns true for the elements in the range.
Syntax:
|
1 2 3 |
bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred); |
Numeric Input Validation in C++
To handle numeric inputs, we can use an extraction operator to check if the user entered the numeric type input or not. With the help of fail(), we can check if the user entered numeric input or not.
Code:
|
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 |
#include <iostream> #include <limits> using namespace std; int main() { int user_age{}; while (true) { cout << "Enter the age: "; cin >> user_age; if (cin.fail()) // If nothing is entered it will be true { cout<<"Enter valid age!!\n"; cin.clear(); // For resetting the bits cin.ignore(numeric_limits<streamsize>::max(), '\n'); //ignore() is used to clear the wrong input entered continue; // Loop will run again } //For clearing additional input like: 23abc(here abc is addtional) cin.ignore(numeric_limits<streamsize>::max(), '\n'); // remove the additional input from the stream if (cin.gcount() > 1) // it will return more than 1 if there is something which is not extracted properly { cout<<"Enter valid age!!\n"; continue; // we'll consider this input to be invalid } if (user_age <= 0) // checking if the age entered is a positive number or not. { cout<<"Enter valid age!!\n"; continue; } break; } cout << "The age you entered is: " << user_age << '\n'; } |
Input:
Enter the age: Java2Blog
Enter the age: 23abc
Output:
Enter valid age!!
Enter valid age!!
Conclusion
In this article, we learnt different ways to implement input validation in C++. Any method can be used as per the requirements.
Happy Learning!!