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

  • Calculator program in Python
    19 June

    Calculator program in Python

    Table of ContentsUsing the while loop along with the if...else conditional statement.Define functions for Addition, Subtraction, Multiplication and DivisionTake user input using input functionComplete calculator program in Python A simple Calculator can be utilized to carry out the four basic arithmetic operations namely addition, division, multiplication, and subtraction depending on the input of the user. […]

  • Convert float to int in Python
    19 June

    Convert float to int in Python

    Table of ContentsUsing the int() function to convert float to int in PythonUsing the math module functions to convert float to int in PythonUsing ceil()Using trunc()Using floor Python provides several in-built functions and modules for datatype conversions. Floating-point numbers can be easily converted to integers using some of these in-built functions. This tutorial will discuss […]

  • Maximum number of vowels in a Substring of given length
    18 June

    Maximum Number of Vowels in a Substring of Given Length

    Table of ContentsApproach – 1 Generate All Substrings Using substring() MethodApproach – 2 Using Sliding Window Method (Linear Time Solution) In this article, we will look at an interesting problem related to the Strings and [Sliding-Window Algorithm](https://java2blog.com/sliding-window-maximum-java/ “Sliding-Window Algorithm”). The problem is : "Given a String we have to Find the Maximum Number of Vowel […]

  • Pandas convert column to int
    18 June

    Pandas convert column to int

    Table of ContentsUse the to_numeric() function to convert column to intUse the astype() function to convert column to intUse the infer_objects() function to convert column to intUse the convert_dtypes() function to convert column to int Pandas is a library set up on top of the Python programming language and is mostly used for the purpose […]

  • Detect keypress in Python
    12 June

    Detect keypress in Python

    Table of ContentsUsing the keyboard module to detect keypress in PythonUsing the pynput module to detect keypress in PythonUsing the msvcrt module to detect keypress in Python Python allows us to work with user input in its programs. We can also work with hardware devices in Python. In this article, we will discuss how to […]

  • Write a list to a file in Python
    12 June

    Write a list to a file in python

    Table of ContentsUsing the write() function to write a list to a file in PythonUsing the writelines() function to write a list to a file in PythonUsing the pickle.dump() function to write a list to a file in PythonUsing the json.dump() function to write a list to a file in PythonUsing the unpack operator * […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.