|
-
Tiger Shark
Error-checking...
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!!
-
Ursus Arctos Moderatis
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.
-
Tiger Shark
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..
Last edited by Thraner; 12-16-2002 at 10:01 PM.
Thraner
-
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
-
Tiger Shark
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!!
-
Ursus Arctos Moderatis
It sounds like you answered your own question buddy 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 can help you test for that period character 
G'luck bro.
-
Tiger Shark
Yep, I think that gets me on the right track!! Thanks again!!
-
Expensive Sushi
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
-
-
NullPointerException
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:
Code:
^[ \011]*[-+]?[0-9]*\\.?[0-9]+([EeDd][-+]?[0-9]+)?[ \011]*$
Open Source is free like a puppy is free.
It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames.
Understanding Evolution
-
Registered User
Re: Error-checking...
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|