Results 1 to 8 of 8

Thread: Is this all numbers?

  1. #1
    Guest

    Post

    How do i find out if argv[1] is all numbers?

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Loop through it, seeing if the characters are in the range '0' to '9'. You may need to include '-' for the first character.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Guest
    Could you supply some code?

  4. #4
    Guest
    Ok this is what i came up with but it doesnt seem to work...

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

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Think you lost your [ i ] there.

    I think it was meant to be like this:
    Code:
    int Digit(char * test)
    {
    	for(int i = 0; i > strlen(test); i++)
    	{
    		if(isdigit(test[ i ]) == 0)
    		{
    			return 1;
    		}
    	}
    
    	return 0;
    }
    Harry.

    "From one thing, know ten thousand things."

  6. #6
    Guest
    I had the i their, but it made it italics instead.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Try this:
    Code:
    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 <.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    I thought I'd try out my asm skills on this one, try this

    Code:
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width