Input Validation in C++

Input Validation in C++

In this post, we will see about Input validation in C++.

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.

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:

Input:

Enter the name of the person: Shashank Jain
Enter the name of the person: Java2Blog
Enter the name of the person: Test123

Output:

The name entered is Shashank Jain
Enter a Valid Name!!
Enter a Valid Name!!

Note: all_of function will return true if pred returns true for the elements in the range.

Syntax:

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:

Input:

Enter the age: 35
Enter the age: Java2Blog
Enter the age: 23abc

Output:

The age you entered is: 35
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!!

Related Posts

  • Make requirements.txt in Python
    03 May

    Make requirements.txt in python

    Table of ContentsWhat is the requirements.txt file in Python?Ways to make requirements.txt file in PythonUsing the pip to make requirements.txt in PythonUsing the conda command to make requirements.txt file in PythonUsing the pipreqs package to make requirements.txt file in PythonConclusion What is the requirements.txt file in Python? Every package is also updated regularly and features […]

  • Create an array of 1 to 10 in Python
    03 May

    Create an Array of 1 to 10 in Python

    Table of ContentsIntroductionHow to create an array of 1 to 10 in Python?Using the range() function to create an array of 1 to 10 in Python.Using list comprehension along with the range() function to create an array of 1 to 10 in Python.Using a user-defined function to create an array of 1 to 10 in […]

  • Remove Urls from String in Python
    03 May

    Remove Urls from Text in Python

    Table of ContentsIntroductionWays to remove URLs from Text in PythonUsing the re.sub() function to remove URLs from Text in PythonUsing the re.findall() function to remove URLs from Text in PythonUsing the re.search() function to remove URLs from Text in PythonUsing the urllib.urlparse class to remove URLs from Text in PythonConclusion In this post, we will […]

  • Create a list from 1 to 100 in Python
    03 May

    Create a List from 1 to 100 in Python

    Table of ContentsWays to create a list from 1 to 100 in PythonUsing the range() function to create a list from 1 to 100 in PythonUsing the numpy.arange() function to create a list from 1 to 100 in PythonUsing the for loop with range() to create a list from 1 to 100 in PythonConclusion In […]

  • How to initialize an array in Constructor in C++
    23 April

    How to initialize an Array in Constructor in C++

    Table of ContentsArraysConstructors of a ClassWays to initialize an array in Constructor in C++Initialize an array in Constructor With std::fill()Initialize an array in Constructor Using for LoopInitialize an array in Constructor Using Member Initializer ListConclusion This article discusses methods of initializing an array in constructor in C++. Let us first discuss arrays and constructors in […]

  • Set an array to another array in java
    23 April

    Set an Array Equal to Another Array in Java

    Table of ContentsSetting an Array Variable Equal to Another Array VariableSet an Array Equal to Another Array in Java Using the clone() MethodSet an Array Equal to Another Array in Java Using the arraycopy() MethodSet an Array Equal to Another Array in Java Using the copyOf() MethodSet an Array Equal to Another Array in Java […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.