|
-
Hammerhead Shark
Numeric Checking in C++
I'm looking for something along the guidelines of IsNumeric() in VB for C++. Yeah, I'm a vb programmer 
------------------
Athlon XP 1800+ (OC pending)
Alpha PAL8045
Epox 8KHA+
256mb Crucial pc2100 ddr
Maxtor D740x 40 GB
ATI Radeon8500 (300/300)
Antec PP403x 400w
-
Mako Shark
.
EDIT: don't know (sorry)
------------------
Keep it brief
[This message has been edited by dighn (edited January 20, 2002).]
-
Texan Dragon Moderator
Originally posted by Raptor2150:
I'm looking for something along the guidelines of IsNumeric() in VB for C++. Yeah, I'm a vb programmer 
What is it that you are trying to accomplish? We might be able to code somethin' for ya.
------------------
Dragon of the OC Crusaders
I only chewed in self-defense.
Dragon of the OC Crusaders
Break the rules and you're snack food for this dragon...
-
Hammerhead Shark
Wow, is it really that difficult in c? I just wanted to check if an inputted variable of type float was actually a numeric value before I tried to do any calculations...
------------------
Athlon XP 1800+ (OC pending)
Alpha PAL8045
Epox 8KHA+
256mb Crucial pc2100 ddr
Maxtor D740x 40 GB
ATI Radeon8500 (300/300)
Antec PP403x 400w
-
Mako Shark
well u could alwasy write one yourself...
this code will make sure the string is in the format -x.x where - . are optional and x can be any number of digits as long as it's at least 1 digit. this is just sort of a demo so there's lots of room for improvement
Code:
bool isNumeric(const char* s)
{
bool needDigit = true;
bool gotDecimal = false;
for(int i = 0; i < strlen(s);i++)
{
if(s[i] < '0' | | s[i] > '9')
{
if(s[i] == '-')
{
if(i != 0)
return false;
}
else if(s[i] == '.')
{
if(needDigit)
return false;
if(gotDecimal)
return false;
needDigit = true;
gotDecimal = true;
}
else
return false;
}
else
needDigit = false;
}
return !needDigit;
}
------------------
Keep it brief
-
Originally posted by Raptor2150:
I'm looking for something along the guidelines of IsNumeric() in VB for C++. Yeah, I'm a vb programmer 
The easiest way is to convert the string to a number using scanf:
Code:
char buffer[256] = "-10";
int num;
sscanf(buffer, "%d", &num);
The trick is that scanf will return the number of arguments that it successfully assigned. Thus:
Code:
if( sscanf(buffer, "%d", &num) != 1 ) {
/* error here */
}
-
Hammerhead Shark
Wow, is it really that complicated? I would think that there would be some sort of function for it. Anyways, thanks all.
------------------
Athlon XP 1800+ (OC pending)
Alpha PAL8045
Epox 8KHA+
256mb Crucial pc2100 ddr
Maxtor D740x 40 GB
ATI Radeon8500 (300/300)
Antec PP403x 400w
-
Yep, C(++) doesn't always provide every little thing that people may want. But, that's why you make your own functions, then it is no longer complicated. ... as long as you have good comments.
-
Uh, if(!isalpha(stuff)), or is that only PHP or something?
------------------
EPoX 8KHA+
Athlon XP 1900+
512 MB DDR-SDRAM
PNY GeForce3 TI 200
EPoX 8KHA+
Athlon XP 1900+
512 MB DDR-SDRAM
PNY GeForce3 TI 200
-
Originally posted by schnarf283:
Uh, if(!isalpha(stuff)), or is that only PHP or something?
Sure, you can use isdigit(...), however that doesn't really handle negative numbers properly. There are regular expression libraries, but these are cumbersome.
so:
Code:
/* returns non-zero if true */
int isAsciiInteger(char *nzstr)
{
int dummy;
return(sscanf(nzstr, "%d", &dummy) == 1);
}
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
|
|