PDA

View Full Version : sscanf checking string help


Riwerry
28/01/2014, 12:20 PM
Hello guys, is possible to edit this:


if(sscanf(inputtext, "{p<_>s[9]s[9]}"))
{
SendClientMessage(playerid, -1, "You have entered wrong format of name!");
}


this checking if is the underscore in inputtext format NAME_LASTNAME it works pretty well but one thing, which doesnt work is: when I enter to inputtext this "_sometext" it says so it is correct, I just need to know I guess, how to get strlen of the string first tring and add if statement if is strlen bigger than 1.. Any ideas? thx

Riwerry
28/01/2014, 03:49 PM
Hm, I still got one problem.. When I type to inputtext as first character the underscore (_) it will detect him.. But when I type after underscore some text (string) like _world it will still detect it as correct.. :( Adding code


case 7:
{
if (response)
{
if(sscanf(inputtext, "{p<_>s[9]s[9]}"))
{
new TEXT[MAX_PLAYER_NAME], underscorecount;
format (TEXT, sizeof (TEXT), "%s", inputtext);
for (new i; i < MAX_PLAYER_NAME; i++)
{
if (TEXT[i] == '_')
underscorecount++;
}

if (underscorecount > 1)
{
SendClientMessage (playerid, -1, "two underscores found!");
}

if (TEXT[0] == '_')
{
SendClientMessage(playerid, -1, "underscore is located in first place in inputtext!");
return ShowPlayerDialog (playerid, 3, DIALOG_STYLE_LIST, "Správca postáv", "Načítavanie postáv\nVytvorenie postáv\nVymazanie Postáv", "Potvrdiť", "Zrušiť");
}

SendClientMessage(playerid, -1, "You have entered bad format of name!");
return ShowPlayerDialog (playerid, 3, DIALOG_STYLE_LIST, "Správca postáv", "Načítavanie postáv\nVytvorenie postáv\nVymazanie Postáv", "Potvrdiť", "Zrušiť");
}
else
{
SendClientMessage(playerid, -1, "Correct format");
ShowPlayerDialog (playerid, 3, DIALOG_STYLE_LIST, "Správca postáv", "Načítavanie postáv\nVytvorenie postáv\nVymazanie Postáv", "Potvrdiť", "Zrušiť");
}
}


Dont worry about dialog lang :D I translated messages / other stuff..

Konstantinos
28/01/2014, 04:06 PM
stock isValidName(const sz_Name[])
{
new
lenght = strlen(sz_Name);

if (!lenght) return 0;

new
us_count;

for (new i; i != lenght; ++i)
{
if (sz_Name[i] == '_') ++us_count;
}

return (us_count == 1 && sz_Name[0] != '_' && sz_Name[lenght - 1] != '_');
}


You can use it:

if (!isValidName(inputtext))
{
SendClientMessage(playerid, -1, "You have entered bad format of name!");
return ShowPlayerDialog (playerid, 3, DIALOG_STYLE_LIST, "Správca postáv", "Načítavanie postáv\nVytvorenie postáv\nVymazanie Postáv", "Potvrdiť", "Zrušiť");
}
else
{
SendClientMessage(playerid, -1, "Correct format");
ShowPlayerDialog (playerid, 3, DIALOG_STYLE_LIST, "Správca postáv", "Načítavanie postáv\nVytvorenie postáv\nVymazanie Postáv", "Potvrdiť", "Zrušiť");
}