Function
#1

Код:
if(IsValidText("Hi there")) return SendClientMessage(playerid, 0xFF0000FF, "Valid Text"); // Correct
if(IsValidText(":> Hello There")) return SendClientMessage(playerid, 0xFF0000FF, "Invailid Text"); // Correct
if(IsValidText("Hello :> There")) return SendClientMessage(playerid, 0xFF0000FF, "Vailid Text"); // Wrong
pawn Код:
stock IsValidText(text[])
{
    for(new i = 0; i < sizeof(text[]); i++)
    {
        if(text[i] == 'a' || text[i] == 'b' || text[i] == 'c' || text[i] == 'd'
        || text[i] == 'e' || text[i] == 'f' || text[i] == 'g' || text[i] == 'h'
        || text[i] == 'i' || text[i] == 'j' || text[i] == 'k' || text[i] == 'l'
        || text[i] == 'm' || text[i] == 'n' || text[i] == 'o' || text[i] == 'p'
        || text[i] == 'q' || text[i] == 'r' || text[i] == 's' || text[i] == 't'
        || text[i] == 'u' || text[i] == 'v' || text[i] == 'w' || text[i] == 'x'
        || text[i] == 'y' || text[i] == 'z')
        return 1;
    }
    return 0;
}
Reply
#2

for(new i = 0; i < sizeof(text); i++)

however I guess that's not the actual problem
Reply
#3

pawn Код:
stock IsValidText(text[])
{
    for(new i = 0; text[i]; i++)
    {
        if(text[i] != 'a' && text[i] != 'b' && text[i] != 'c' && text[i] != 'd'
        && text[i] != 'e' && text[i] != 'f' && text[i] != 'g' && text[i] != 'h'
        && text[i] != 'i' && text[i] != 'j' && text[i] != 'k' && text[i] != 'l'
        && text[i] != 'm' && text[i] != 'n' && text[i] != 'o' && text[i] != 'p'
        && text[i] != 'q' && text[i] != 'r' && text[i] != 's' && text[i] != 't'
        && text[i] != 'u' && text[i] != 'v' && text[i] != 'w' && text[i] != 'x'
        && text[i] != 'y' && text[i] != 'z')
        return 0;
    }
    return 1;
}
Reply
#4

won't that be an endless loop?
Reply
#5

pawn Код:
stock IsValidText(const text[])
{
    for(new i = 0; i < sizeof(text); i++)
        if(!('A' <= text[i] <= 'Z') && !('a' <= text[i] <= 'z')) return 0;

    return 1;
}
ASCII table: http://www.asciitable.com/index/asciifull.gif
Reply
#6

pawn Код:
indeterminate array size in "sizeof" expression (symbol "")
if i change
pawn Код:
for(new i = 0; i < sizeof(text); i++)
to
pawn Код:
for(new i = 0; i < sizeof(text[]); i++)
i will repove the warning but i will get save bug at my 1 post
Reply
#7

Sorry, this one should work. Tested it.

pawn Код:
IsValidText(const text[])
{
    for(new i = 0, j = strlen(text); i < j; i++)
        if(!('A' <= text[i] <= 'Z') && !('a' <= text[i] <= 'z')) return 0;

    return 1;
}
Reply
#8

Quote:
Originally Posted by Sascha
Посмотреть сообщение
won't that be an endless loop?
No, it will stop when reached to the end of the string.

Doing
pawn Код:
for(new i = 0; text[i]; i++)
is same as
pawn Код:
for(new i = 0; text[i] != 0; i++)
same as
pawn Код:
for(new i = 0; text[i] != EOS; i++)
same as
pawn Код:
for(new i = 0; text[i] != '\0'; i++)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)