PDA

Click to See Complete Forum and Search --> : Is this all numbers?


Nov 15th, 2000, 02:01 PM
How do i find out if argv[1] is all numbers?

parksie
Nov 15th, 2000, 02:08 PM
Loop through it, seeing if the characters are in the range '0' to '9'. You may need to include '-' for the first character.

Nov 15th, 2000, 03:07 PM
Could you supply some code?

Nov 15th, 2000, 04:39 PM
Ok this is what i came up with but it doesnt seem to work...


int Digit(char * test)
{
for(int i = 0; i > strlen(test); i++)
{
if(isdigit(test[i]) == 0)
{
return 1;
}
}

return 0;
}

HarryW
Nov 16th, 2000, 08:43 AM
Think you lost your [ i ] there.

I think it was meant to be like this:

int Digit(char * test)
{
for(int i = 0; i > strlen(test); i++)
{
if(isdigit(test[ i ]) == 0)
{
return 1;
}
}

return 0;
}

Nov 16th, 2000, 02:20 PM
I had the i their, but it made it italics instead.

parksie
Nov 16th, 2000, 04:26 PM
Try this:

bool Digit(char *test) {
for(int j = 0; j < strlen(test); j++) {
if(isdigit(test[j]) == 0) {
return true;
}
}

return false;
}

I think you got your comparison mixed up between > and <.

Sam Finch
Nov 16th, 2000, 09:09 PM
I thought I'd try out my asm skills on this one, try this


bool StringNumeric(char* MyString)
{
__asm
{
push ebx
push ecx
push ax


mov ax, 0x2dfe ;ah = '-' al = ('.' - 0x30)
mov ebx, MyString
mov ecx, 0 ;ch must be zero for the jcxz instruction to work (ecx is 0 as we must add it to ebx)

sub ah, [ebx] ;subtract the first character from ah (so ah = 0 if the string begins with a '-')
sete cl ;if the 1st character is '-' ecx = 1, else ecx = 0
add ebx, ecx ;adds ecx to ebx


loopstrt:

mov cl, [ebx] ;store the curren character in cl
inc ebx ;move to the next character
jcxz stringfinished ;if we find a null char exit the loop

sub cl, 0x30 ;cl should be between 30 and 39, so subtract 30 and check it's below 9
cmp cl, 9
jb loopstrt

sub al, cl ;compare cl to al (true if cl was '.' originally, set al to 0 if true
jz loopstrt ;(if it's false al could be anything but we return false anyway

stringfinished:
pop ax

cmp cl, 0 ;if cl = 0 return true else return false
sete al

pop cx
pop ebx
}
}


i reckon that's probably the fastest way of doing it.