containsLetters(string[])
#1

Sup guys!

I'm trying to make a function that checks if there's Letters in a string, cause in my Dialog, I only accept number 0~9.

pawn Код:
stock onlyNumbers(string[])
{
    new bool:contains = false;
    for(new c = 0; c < sizeof(string); c++)
    {
        new character = strval(string(c));
        if(character >= '0' && character <= '9')
        {
            // do nothing
        }
        else
        {
            contains = true;
            return false;
            break;
        }
    }
    if(!contains) return true;
}
But I'm getting these errors:
Код:
C:\Users\Gustavo\Desktop\samp03csvr_R2-2_win32\gamemodes\fcrp.pwn(1551) : warning 224: indeterminate array size in "sizeof" expression (symbol "")
C:\Users\Gustavo\Desktop\samp03csvr_R2-2_win32\gamemodes\fcrp.pwn(1553) : error 012: invalid function call, not a valid address
C:\Users\Gustavo\Desktop\samp03csvr_R2-2_win32\gamemodes\fcrp.pwn(1553) : warning 215: expression has no effect
C:\Users\Gustavo\Desktop\samp03csvr_R2-2_win32\gamemodes\fcrp.pwn(1553) : error 001: expected token: ";", but found ")"
C:\Users\Gustavo\Desktop\samp03csvr_R2-2_win32\gamemodes\fcrp.pwn(1553) : error 029: invalid expression, assumed zero
C:\Users\Gustavo\Desktop\samp03csvr_R2-2_win32\gamemodes\fcrp.pwn(1553) : fatal error 107: too many error messages on one line
Reply
#2

pawn Код:
stock onlyNumbers(string[],len=0)
{
    len = strlen(string);
    for(new c = 0; c < len; c++)
    {
        if(!('0' <= string[c] <= '9')) return false;
    }
    return true;
}
Reply
#3

pawn Код:
stock IsNumeric(const str[])
{
    new len = strlen(str);
   
    if(!len) return false;
    for(new i; i < len; i++)
    {
        if(!('0' <= str[i] <= '9')) return false;
    }
    return true;
}
Too late. Again.
Reply
#4

Thanks guys, you're a genius.
Reply
#5

You could simply use the stock IsNumeric, to determine if the string is completely numeric, if it returns false, there are characters or letters in it.

Usage;
if(!IsNumeric(string))

pawn Код:
stock IsNumeric(str[])
{
    new ch, i;
    while ((ch = str[i++])) if (ch < '0' || ch > '9') return 0;
    return 1;
}
EDIT: Beaten twice in a row, both of you good job. I seriously need to refresh before posting.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)