How to filter a value?
#1


I'd like to know how to filter a value for my birth date system.

1st : A player would enter in a command or whatever some text.
2nd : I'd like to make sure he's using the good format (dd/mm/yyyy)
3rd : Then, I would separate them, removing all the slashes ( / ) into 3 different values

My problem comes after I get the text entered by the player. I don't know what to do for steps 2 and 3.

Thanks for helping!
Reply
#2

You only need to split the string

pawn Code:
new day, month, year = -1 = month = day;
for(new c = 0/* Startpoint */, s = c; text[c] != EOS; c++) //the text should start at the date
    if(text[c] == '/')
    {
        if(day == -1)
        {
            if((c - s) > 2) break;
            for(day = 0; s < c; s++)
                day = day * 10 + (text - '0');
            s++;
        }
        else if(month == -1)
        {
            if((c - s) > 2) break;
            for(month = 0; s < c; s++)
                month = month * 10 + (text - '0');
            s++;
        }
    }
    else if(text[c] == ' ')
    {
        if(month == -1 || day == -1) break;
        else if((c - s) > 4) break;
        for(year = 0; s < c; s++)
            year = year * 10 + (text - '0');
        break;
    }
    else if(!('0' <= text[c] <= '9')) break;
if(year == -1 || month == -1 || day == -1) return 0;
if you use it
at OnPlayerText
pawn Code:
if(PlayerTypesDate[playerid])
{
    if(!('0' <= text[0] <= '9')) return SendClientMessage(playerid, 0xFFFFFFAA, "Enter the date in the format dd/mm/yyyy");
    //code above - c = 0
}
at OnPlayerCommandText
pawn Code:
if(strcmp("/command", cmdtext, true, 8 /* strlen("/command") */) == 0)
{
    if(cmdtext[8/* strlen("/command") */] != ' ' || cmdtext[9/* strlen("/command") + 1 */] == EOS)
        return SendClientMessage(playerid, 0xFFFFFFAA, "Enter the date in the format dd/mm/yyyy");
    //code above - c = 9 /* strlen("/command") + 1 */
}
Reply
#3

Quote:
Originally Posted by ♣ Joker ♠
You only need to split the string

pawn Code:
new day, month, year = -1 = month = day;
for(new c = 0/* Startpoint */, s = c; text[c] != EOS; c++) //the text should start at the date
    if(text[c] == '/')
    {
        if(day == -1)
        {
            if((c - s) > 2) break;
            for(day = 0; s < c; s++)
                day = day * 10 + (text - '0');
            s++;
        }
        else if(month == -1)
        {
            if((c - s) > 2) break;
            for(month = 0; s < c; s++)
                month = month * 10 + (text - '0');
            s++;
        }
    }
    else if(text[c] == ' ')
    {
        if(month == -1 || day == -1) break;
        else if((c - s) > 4) break;
        for(year = 0; s < c; s++)
            year = year * 10 + (text - '0');
        break;
    }
    else if(!('0' <= text[c] <= '9')) break;
if(year == -1 || month == -1 || day == -1) return 0;
if you use it
at OnPlayerText
pawn Code:
if(PlayerTypesDate[playerid])
{
    if(!('0' <= text[0] <= '9')) return SendClientMessage(playerid, 0xFFFFFFAA, "Enter the date in the format dd/mm/yyyy");
    //code above - c = 0
}
at OnPlayerCommandText
pawn Code:
if(strcmp("/command", cmdtext, true, 8 /* strlen("/command") */) == 0)
{
    if(cmdtext[8/* strlen("/command") */] != ' ' || cmdtext[9/* strlen("/command") + 1 */] == EOS)
        return SendClientMessage(playerid, 0xFFFFFFAA, "Enter the date in the format dd/mm/yyyy");
    //code above - c = 9 /* strlen("/command") + 1 */
}
Thanks alot, but I don't get something.
In the second piece of code, there's
Code:
// code above - c = 0
What does that mean? (EDIT: talking about the '- c = 0')
Reply
#4

Quote:
Originally Posted by X Cutter
What does that mean? (EDIT: talking about the '- c = 0')
that was related to

Quote:
Originally Posted by ♣ Joker ♠
pawn Code:
for(new c = 0/* Startpoint */, s = c; text[c] != EOS; c++) //the text should start at the date
the loop should start at the date
it would stop if the loop gets called before
so I wrote the examples, how to find out the start value
Reply
#5

Quote:
Originally Posted by ♣ Joker ♠
Quote:
Originally Posted by X Cutter
What does that mean? (EDIT: talking about the '- c = 0')
that was related to

Quote:
Originally Posted by ♣ Joker ♠
pawn Code:
for(new c = 0/* Startpoint */, s = c; text[c] != EOS; c++) //the text should start at the date
the loop should start at the date
it would stop if the loop gets called before
so I wrote the examples, how to find out the start value
Okay, thanks, gonna try this atm.
Reply
#6

Tested it. Compiles well except 1 error.
I get
Code:
error 022: must be lvalue (non-constant)
on line:
Code:
new day, month, year = -1 = month = day;
Reply
#7

Quote:
Originally Posted by X Cutter
Tested it. Compiles well except 1 error.
I get
Code:
error 022: must be lvalue (non-constant)
on line:
Code:
new day, month, year = -1 = month = day;
oh ye, it should be
pawn Code:
new day, month, year = month = day = -1;
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)