How to filter a value? - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (
https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: How to filter a value? (
/showthread.php?tid=118037)
How to filter a value? -
x-cutter - 02.01.2010
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!
Re: How to filter a value? -
Nero_3D - 02.01.2010
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 */
}
Re: How to filter a value? -
x-cutter - 02.01.2010
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')
Re: How to filter a value? -
Nero_3D - 03.01.2010
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
Re: How to filter a value? -
x-cutter - 03.01.2010
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.
Re: How to filter a value? -
x-cutter - 03.01.2010
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;
Re: How to filter a value? -
Nero_3D - 03.01.2010
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;