Click to See Complete Forum and Search --> : Error-checking...


Thraner
12-16-2002, 05:43 PM
Hey guys, I am new to C++, and for a project I am working on, I need to check a value entered by a user to see if it is a valid number. I know this would be easy as pie in VB by using the function "IsNumeric()", but I can't figure out how to do something like this in C++. Any suggestions?
Thanks in advance!!

Grizzly
12-16-2002, 07:27 PM
You could fairly easily write your own function that accepts a character, and analyzes the ascii character code associated with that character. If the ascii character code of the supplied char falls between (and including) 48-57, than you know it is numeric.

Thraner
12-16-2002, 08:39 PM
I thought of that too, but I think I am going to be checking for a possible numeric value, which means I would need to parse the float that is sent to the function, is that correct?

EDIT: OK, I guess the more I think of it, I can really only parse a string value, or can I force a float to a string value then parse that? I am kinda stumped here..

Frostburn
12-16-2002, 11:33 PM
If a user enters a string, you could iterate through the string and check to see if every character is a number like grizzly said and check if it has a period (only 1 though.)

Have you got into strings at all? If so, I'll be able to help you further, otherwise im just doing your work =p

Thraner
12-17-2002, 12:37 AM
I know a good amount about working with strings from VB. Also, I just want to say forgive me if I seem like I want you to do my work for me. I am just looking for a little guidance/suggestions on the best/easiest way to do this. The coding I can handle.
So, anyhow, are these the steps I should take? :

1) Collect the user's input as a string instead of a float
2) Pass the user's input to the function I will create
3) Parse the entire string making sure there are only numerals and possibly one decimal point, and if so, force the string to a float and return it to the main function, otherwise return an error

Guys, I think this may work, I think I just had to work it out on here.. Thanks for your help!!

Grizzly
12-17-2002, 09:47 AM
It sounds like you answered your own question buddy :p I will just say this:

I would create 2 functions. One, called something like "isNumeric()" that accepts a single char and returns true if it's a numeric value, and false if it is not.

The other function would be the function that accepts a string, name that whatever you want, maybe..."isNumericString()" or something along those lines. Which takes an entire string, and evaluates each individual character in that string with the "isNumeric()" function. If any of the characters are not numeric, OR more than one of the characters is a period, than return false. Otherwise, return true.

Ascii Table dot com (http://www.asciitable.com/) can help you test for that period character :)

G'luck bro.

Thraner
12-17-2002, 10:05 AM
Yep, I think that gets me on the right track!! Thanks again!!

Medicated
12-17-2002, 03:13 PM
c++ has a built in function for checking if a char is a number, isdigit(), think its in ctype.h, you could also write the function yourself with something like

if ( '0' <= c && c <= '9' )

assuming your char is named c

but my book says thats less efficient than the built in functions and can cause errors on some systems. you should note that the isdigit() functions requires an unsigned char and some systems use a signed char as the standard char type, to get around this, you should be able to just cast your var to an unsigned char, like

isdigit((unsigned char)c)

then the system should handle the conversion for you.

also, i'm not completely sure what you are trying to do, but wouldn't it be easier to just read the number into a float, and then check to see if the value is valid(for error checking, set the float to an invalid value before you read input)

Medicated

Conrad Song
12-19-2002, 12:46 AM
I felt a bit of deja-vu:

http://www.sharkyforums.com/showthread.php?s=&threadid=92731

rock
12-19-2002, 09:51 AM
If you're just looking for simple numerics, the above suggestions are fine. If you want to allow more general numbers (1.5e3) you'll need to use extra care.

In fact, you might want to consider using regular expressions and compare to this the input to this string:

^[ \011]*[-+]?[0-9]*\\.?[0-9]+([EeDd][-+]?[0-9]+)?[ \011]*$

peterlak
12-19-2002, 08:46 PM
Originally posted by Thraner
Hey guys, I am new to C++, and for a project I am working on, I need to check a value entered by a user to see if it is a valid number. I know this would be easy as pie in VB by using the function "IsNumeric()", but I can't figure out how to do something like this in C++. Any suggestions?
Thanks in advance!!

could you make your question more spicifi provideing the details on how the input will happen? are you going to write your own funcion? are you using cin. are you using a char array or numerical datatypes?