SA-MP Forums Archive
Checking if inputtext is 4 digits. - 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)
+--- Thread: Checking if inputtext is 4 digits. (/showthread.php?tid=591234)



Checking if inputtext is 4 digits. - faff - 09.10.2015

Evening,

I want to check if the pincode an administrator selected is 4 digits long.

I tried to do that this way.

PHP код:
        if(sscanf(params"d"pass)) return SendClientMessage(playeridCOLOR_WHITE"USAGE: /changeadminpin [Pin code]");
        if(
strlen(pass) < || strlen(pass) > 4) return SendClientMessage(playeridCOLOR_WHITE"SERVER: The pincode must be 4 digits."); 
But this is not working, if I try to compile it is showing me these errors.
Код:
C:\Users\fabio\Desktop\International Roleplay\gamemodes\I-RP.pwn(2306) : error 035: argument type mismatch (argument 1)
C:\Users\fabio\Desktop\International Roleplay\gamemodes\I-RP.pwn(2328) : error 035: argument type mismatch (argument 1)
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


2 Errors.



Re: Checking if inputtext is 4 digits. - ThePhenix - 09.10.2015

Quote:
Originally Posted by faff
Посмотреть сообщение
Evening,

I want to check if the pincode an administrator selected is 4 digits long.

I tried to do that this way.

PHP код:
        if(sscanf(params"d"pass)) return SendClientMessage(playeridCOLOR_WHITE"USAGE: /changeadminpin [Pin code]");
        if(
strlen(pass) < || strlen(pass) > 4) return SendClientMessage(playeridCOLOR_WHITE"SERVER: The pincode must be 4 digits."); 
But this is not working, if I try to compile it is showing me these errors.
Код:
C:\Users\fabio\Desktop\International Roleplay\gamemodes\I-RP.pwn(2306) : error 035: argument type mismatch (argument 1)
C:\Users\fabio\Desktop\International Roleplay\gamemodes\I-RP.pwn(2328) : error 035: argument type mismatch (argument 1)
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


2 Errors.
You are not declaring pass as a string. In order to use strlen 'pass" should be a string.


Re: Checking if inputtext is 4 digits. - Crayder - 09.10.2015

There are better ways, but rearranging your code will do fine:
pawn Код:
if(strlen(params) != 4) return SendClientMessage(playerid, COLOR_WHITE, "SERVER: The pincode must be 4 digits.");
        if(sscanf(params, "d", pass)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /changeadminpin [Pin code]");



Re: Checking if inputtext is 4 digits. - faff - 09.10.2015

Solved.


Re: Checking if inputtext is 4 digits. - faff - 09.10.2015

Weird, It works perfect now but if I set someone else or mine admin pin to "0000" the "SendClientMessage" displays "0". How to solve this?

Nevermind, solved it.


Re: Checking if inputtext is 4 digits. - faff - 09.10.2015

Another problem popped up, this is the code now:
PHP код:
CMD:changeadminpin(playeridparams[])
{
    new 
pass[4], string[128];
    if(
PlayerInfo[playerid][pAdmin] >= 1)
    {
        if(
sscanf(params"s[4]"pass)) return SendClientMessage(playeridCOLOR_WHITE"USAGE: /changeadminpin [Pin code]");
        if(
strlen(pass) != 4) return SendClientMessage(playeridCOLOR_WHITE"SERVER: The pincode must be 4 digits.");
        
sscanf(pass"i"PlayerInfo[playerid][pSecKey]);
         
format(stringsizeof(string), "{FFFFFF}SERVER: You've successfully changed your pin code to %s."pass);
         
SendClientMessage(playeridCOLOR_WHITEstring);
    }
    else return 
SendClientMessage(playeridCOLOR_GRAD2NOTADMIN);
    return 
1;

But even if I only type in 4 digits. it still gives the "SERVER:" message, and doesn't set the pin.


Re: Checking if inputtext is 4 digits. - Ahmad45123 - 09.10.2015

Make sure to save the pin as a string because numbers that start with a zero will bug out.. like "0000" or "0123"..

It should be:
PHP код:
strcpy(passPlayerInfo[playerid][pSecKey]); 
instead of sscanf.. for strings.

And don't forget to change all instances of integer to a 4 digit string.


Re: Checking if inputtext is 4 digits. - faff - 09.10.2015

Quote:
Originally Posted by Ahmad45123
Посмотреть сообщение
Make sure to save the pin as a string because numbers that start with a zero will bug out.. like "0000" or "0123"..

It should be:
PHP код:
strcpy(passPlayerInfo[playerid][pSecKey]); 
instead of sscanf.. for strings.

And don't forget to change all instances of integer to a 4 digit string.
Not what I asked for, check my last message.


Re: Checking if inputtext is 4 digits. - Crayder - 09.10.2015

Quote:
Originally Posted by Ahmad45123
Посмотреть сообщение
Make sure to save the pin as a string because numbers that start with a zero will bug out.. like "0000" or "0123"..

It should be:
PHP код:
strcpy(passPlayerInfo[playerid][pSecKey]); 
instead of sscanf.. for strings.

And don't forget to change all instances of integer to a 4 digit string.
False, it will not bug out. If you want to print the leading zero's you can easily do so.

Quote:
Originally Posted by faff
Посмотреть сообщение
Not what I asked for, check my last message.
You shouldn't use two sscanf strings for that. If you'd do it like I said it would be better. It's your code however, do as you will.

Now about the leading zero's. You shouldn't store it as a string to get them. You said:
Quote:

Weird, It works perfect now but if I set someone else or mine admin pin to "0000" the "SendClientMessage" displays "0". How to solve this?

The correct way to do this would be this (example):
pawn Код:
printf("This number will have three leading zeros: %4i", 0);
//Output: "This number will have three leading zeros: 0000"

printf("These numbers will all be four digits, with leading zeros if needed: %4i, %4i, %4i", 12, 555, 045);
//Output: "These numbers will all be four digits, with leading zeros if needed: 0012, 0555, 0045"



Re: Checking if inputtext is 4 digits. - Ahmad45123 - 09.10.2015

Quote:
Originally Posted by Crayder
Посмотреть сообщение
False, it will not bug out. If you want to print the leading zero's you can easily do so.



You shouldn't use two sscanf strings for that. If you'd do it like I said it would be better. It's your code however, do as you will.

Now about the leading zero's. You shouldn't store it as a string to get them. You said:The correct way to do this would be this (example):
pawn Код:
printf("This number will have three leading zeros: %4i", 0);
//Output: "This number will have three leading zeros: 0000"

printf("These numbers will all be four digits, with leading zeros if needed: %4i, %4i, %4i", 12, 555, 045);
//Output: "These numbers will all be four digits, with leading zeros if needed: 0012, 0555, 0045"
Oh.. That's something new to me tbh.. :P