Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I need a C++ code snippet to validate values entered by user to be of type int. I know that the visual studio compiler will use the ASCII equivalent for a char if the user enters characters but I do not want the input to be characters or any other type. The algorithm for the code is something like:
if (input == integer)
accept;
else
reject;
The problem I have presently is that a value and a type cannot be tested for equality.
Posted

1 solution

Everything that is entered by the user is in the form of character strings, it is up to your application to decide what type it represents and to convert it. Use one of the integer parsing functions, such as strtol[^] etc, and reject any input that is not a valid number.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900