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

  • 17 September

    Spectrogram in Python

    Table of ContentsSpectrogramPlotting Spectrogram using the matplotlib.pyplot.specgram function of the Matplotlib Library.Plotting Spectrogram using the scipy.signal.spectrogram function of the SciPy Library Spectrogram A spectrogram is a wave-like graph which is used to represent measures like loudness, frequencies, and other signals that change over time. With the help of a spectrogram, these signals and measures are […]

  • 17 September

    27 Best and Safe ROM Sites in 2021

    Table of ContentsThe Best and Safe ROM Sites in 2021DopeROMsCOOL ROMROMsmaniaGamulatorROM HustlerRetrosticROMspediaROMs WorldROM Nation ROMUIationROMs UniverseClassic GameROMsCDROMance Emulator Game     EmuparadiseWoWROMsEmulator ZoneNitroblogVimm’s LairFree ROMsOther Working Safe Rom Sites in 2021Frequently Asked Questions Are there any free and safe ROM sites?What are the safest ROM sites?Is it legal to download a ROM?Wrapping Up Do you still wish to play […]

  • 17 September

    Write list to CSV in Python

    Table of ContentsUsing the write() function to write a list to a CSV fileUsing the csv module to write a list to a CSV fileUsing the numpy module to write a list to a CSV fileUsing the numpy.savetxt() function to write a list to a CSV fileUsing the numpy.tofile() function to write a list to […]

  • Python add commas to number
    11 September

    Python add commas to number

    Table of ContentsUsing the format() function to add commas to numbers in PythonUsing the fstrings to add commas to numbers in PythonUsing the regular expressions to add commas to numbers in PythonUsing the locale module to add commas to numbers in Python Python allows us to format values to get the final result in our […]

  • 11 September

    RSA Encryption and Decryption in Java

    Table of ContentsIntroductionGenerate RSA key pairEncrypt a random textDecrypt the random textConclusion Introduction RSA is a short form for Rivest, Shamir, and Adleman, are the people who first publicly described it in 1977. It is an algorithm for asymmetric cryptography which involves the use of two keys. A public key, which can be known to […]

  • 10 September

    How to remove element from Arraylist in java while iterating

    Table of ContentsIntroductionUsing Collection’s removeIf() methodUsing ListIterator classUsing removeAll() methodUsing Java 8 Stream to filter List itemsConclusion Introduction In this tutorial, you will learn how to remove element from Arraylist in java while iterating using different implementations provided by Java. It is necessary to understand the right way to remove items from a List because […]

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.