Issue with SScanf and string
#1

Hello.
I have an issue with sscanf and storing a note in a enum.
this is my code
PHP код:
/* ============================================================================ */
COMMAND:barrier(playeridparams[])
{
    if(!
Player[playerid][Authed]) return SendClientError(playerid"You are not authed!");
    if(!
IsFed(playerid)) return SendClientError(playerid,"You are not authorized to use this command!");
    new 
tmp15 ], tmp215 ],tmp3[200];
    if(
sscanf(params"szzz"tmptmp2,tmp3))
        {
            
SendClientUsage(playerid,"/barrier [ Deploy / Destroy / List]");
        }
    else if(!
strcmp(tmp"deploy"true5))
        {
            new 
type;
             if(!
strlen(tmp2)) return SendClientUsage(playerid"/barrier deploy [1/2/3/4] [Note]");
             new 
barriertype strval(tmp2);
             if(
barriertype == 1)
                 {
                 
type=978;
                 }
             else if(
barriertype == 2)
                 {
                 
type=978;
                 }
             else if(
barriertype == 3)
                 {
                 
type=978;
                 }
            else if(
barriertype == 4)
                {
                
type=978;
                }
            else
                {
                return 
SendClientUsage(playerid"/barrier deploy [1/2/3/4] [Note]");
                }
            for(new 
1MAX_BARRIERSi++)
                {
                    if(!
Barrier[i][bActive]) 
                        {
                            new 
Float:XFloat:YFloat:ZFloat:A;
                            new 
string[400];
                            
GetPlayerPos(playeridXYZ);
                            
GetPlayerFacingAngle(playeridA);
                            
Barrier[i][bObject] = CreateDynamicObject(typeXYZ0.00.0AGetPlayerVirtualWorld(playerid), GetPlayerInterior(playerid));
                            
Barrier[i][bLabel] = Create3DTextLabel(sprintf("Barrier ID %i",i),-1,X,Y,Z,10,GetPlayerVirtualWorld(playerid),0);
                            
Barrier[i][bActive] = 1;
                            
format(string,sizeof(string),"%s",tmp3);
                            
myStrcpy(Barrier[i][Note],string);
                            
SetPlayerPos(playeridX-1YZ+2);
                            
SendClientMessage(playerid,-1,sprintf("Barrier ID %i has been deployed",i));
                            break;
                        }
                }
            }
    else if(!
strcmp(tmp"destroy"true5))
        {
            new 
bid strval(tmp2);
            if(
bid || bid MAX_BARRIERS) return SendClientError(playerid,"Invalid barrier id.");
            if(!
Barrier[bid][bActive]) return SendClientError(playerid,"That barrier isn't deployed.");
            
DestroyDynamicObject(Barrier[bid][bObject]);
            
Delete3DTextLabel(Barrier[bid][bLabel]);
            
Barrier[bid][bActive] = 0;
            
SendClientMessage(playerid,-1,sprintf("Barrier ID %i has been destroyed.",bid));
        }
    else if(!
strcmp(tmp"list"true5))
    {
        for(new 
1MAX_BARRIERSi++)
            {
                if(
Barrier[i][bActive]) 
                    {
                        
SendClientMessage(playerid,-1,sprintf("Barrier ID %i | Note: %s",i,Barrier[i][Note]));
                    }
            }
    }
    else
        {
            
SendClientUsage(playerid,"/barrier [ Deploy / Destroy / List]");
        }
    return 
1;
}
/* ============================================================================= */ 
So, Barrier[i][Note] only stores the first word, instead of the whole string.
What's the problem and how to fix it?
Reply
#2

I'm not sure why you're using the "z" specifier since it is deprecated, and you're not even specifying the string length next to it (example, z[15]). Also, you only have 1 parameter (with 3 options) and telling sscanf you need 4 parameters. Then, when you want to set the Note variable value you create a variable called string with 400 cells to insert a 200 cell string in it. That step is also unnecessary, you should just set Note value to tmp.

Try using the "s" sscanf specifier, remove the other 3 specifiers and specify the array size.
Reply
#3

PHP код:
if(sscanf(params"s[15]zzz"tmptmp2,tmp3)) 
You should mention the size of the string. Also, Make sure you add the string size in your Enum.
Reply
#4

Quote:
Originally Posted by DarkSkull
Посмотреть сообщение
PHP код:
if(sscanf(params"s[15]zzz"tmptmp2,tmp3)) 
You should mention the size of the string. Also, Make sure you add the string size in your Enum.
doesnt work
Reply
#5

PHP код:
new tmp15 ],tmp2,tmp3[200]; 
    if(
sscanf(params"s[15]is[200]"tmptmp2,tmp3)) 
Reply
#6

Quote:
Originally Posted by justjamie
Посмотреть сообщение
doesnt work
If you don't help us we can't help you! I barely could realize what you're trying to achieve.

pawn Код:
/* ============================================================================ */
COMMAND:barrier(playerid, params[])
{
    if(!Player[playerid][Authed]) return SendClientError(playerid, "You are not authed!");
    if(!IsFed(playerid)) return SendClientError(playerid,"You are not authorized to use this command!");
    new tmp[ 15 ], tmp2[ 64 ], barriertype;
    if(sscanf(params, "s[15]DS[64]", tmp, barriertype, tmp2))
        {
            SendClientUsage(playerid,"/barrier [ Deploy / Destroy / List]");
                    return 1;
        }
    else if(!strcmp(tmp, "deploy", true, 5))
        {  
             if(sscanf(params, "s[15]ds[64]", tmp, barriertype, tmp2)) return SendClientUsage(playerid, "/barrier deploy [1/2/3/4] [Note]");
 
             if(barriertype == 1)
                 {
                 type=978;
                 }
             else if(barriertype == 2)
                 {
                 type=978;
                 }
             else if(barriertype == 3)
                 {
                 type=978;
                 }
            else if(barriertype == 4)
                {
                type=978;
                }
            else
                {
                return SendClientUsage(playerid, "/barrier deploy [1/2/3/4] [Note]");
                }
            for(new i = 1; i < MAX_BARRIERS; i++)
                {
                    if(!Barrier[i][bActive])  
                        {
                            new Float:X, Float:Y, Float:Z, Float:A;
                            GetPlayerPos(playerid, X, Y, Z);
                            GetPlayerFacingAngle(playerid, A);
                            Barrier[i][bObject] = CreateDynamicObject(type, X, Y, Z, 0.0, 0.0, A, GetPlayerVirtualWorld(playerid), GetPlayerInterior(playerid));
                            Barrier[i][bLabel] = Create3DTextLabel(sprintf("Barrier ID %i",i),-1,X,Y,Z,10,GetPlayerVirtualWorld(playerid),0);
                            Barrier[i][bActive] = 1;
                            myStrcpy(Barrier[i][Note],tmp2);
                            SetPlayerPos(playerid, X-1, Y, Z+2);
                            SendClientMessage(playerid,-1,sprintf("Barrier ID %i has been deployed",i));
                            break;
                        }
                }
            }
    else if(!strcmp(tmp, "destroy", true, 5))
        {
            if(sscanf(params, "s[15]dS[64]", tmp, barriertype, tmp2)) return SendClientUsage(playerid, "/barrier destroy [id]");
            new bid = barriertype;
            if(bid < 1 || bid > MAX_BARRIERS) return SendClientError(playerid,"Invalid barrier id.");
            if(!Barrier[bid][bActive]) return SendClientError(playerid,"That barrier isn't deployed.");
            DestroyDynamicObject(Barrier[bid][bObject]);
            Delete3DTextLabel(Barrier[bid][bLabel]);
            Barrier[bid][bActive] = 0;
            SendClientMessage(playerid,-1,sprintf("Barrier ID %i has been destroyed.",bid));
        }
    else if(!strcmp(tmp, "list", true, 5))
    {
        for(new i = 1; i < MAX_BARRIERS; i++)
            {
                if(Barrier[i][bActive])  
                    {
                        SendClientMessage(playerid,-1,sprintf("Barrier ID %i | Note: %s",i,Barrier[i][Note]));
                    }
            }
    }
    else
        {
            SendClientUsage(playerid,"/barrier [ Deploy / Destroy / List]");
        }
    return 1;
}
/* ============================================================================= */
Not tested.
Reply
#7

Quote:
Originally Posted by Marricio
Посмотреть сообщение
If you don't help us we can't help you! I barely could realize what you're trying to achieve.

pawn Код:
/* ============================================================================ */
COMMAND:barrier(playerid, params[])
{
    if(!Player[playerid][Authed]) return SendClientError(playerid, "You are not authed!");
    if(!IsFed(playerid)) return SendClientError(playerid,"You are not authorized to use this command!");
    new tmp[ 15 ], tmp2[ 64 ], barriertype;
    if(sscanf(params, "s[15]DS[64]", tmp, barriertype, tmp2))
        {
            SendClientUsage(playerid,"/barrier [ Deploy / Destroy / List]");
                    return 1;
        }
    else if(!strcmp(tmp, "deploy", true, 5))
        {  
             if(sscanf(params, "s[15]ds[64]", tmp, barriertype, tmp2)) return SendClientUsage(playerid, "/barrier deploy [1/2/3/4] [Note]");
 
             if(barriertype == 1)
                 {
                 type=978;
                 }
             else if(barriertype == 2)
                 {
                 type=978;
                 }
             else if(barriertype == 3)
                 {
                 type=978;
                 }
            else if(barriertype == 4)
                {
                type=978;
                }
            else
                {
                return SendClientUsage(playerid, "/barrier deploy [1/2/3/4] [Note]");
                }
            for(new i = 1; i < MAX_BARRIERS; i++)
                {
                    if(!Barrier[i][bActive])  
                        {
                            new Float:X, Float:Y, Float:Z, Float:A;
                            GetPlayerPos(playerid, X, Y, Z);
                            GetPlayerFacingAngle(playerid, A);
                            Barrier[i][bObject] = CreateDynamicObject(type, X, Y, Z, 0.0, 0.0, A, GetPlayerVirtualWorld(playerid), GetPlayerInterior(playerid));
                            Barrier[i][bLabel] = Create3DTextLabel(sprintf("Barrier ID %i",i),-1,X,Y,Z,10,GetPlayerVirtualWorld(playerid),0);
                            Barrier[i][bActive] = 1;
                            myStrcpy(Barrier[i][Note],tmp2);
                            SetPlayerPos(playerid, X-1, Y, Z+2);
                            SendClientMessage(playerid,-1,sprintf("Barrier ID %i has been deployed",i));
                            break;
                        }
                }
            }
    else if(!strcmp(tmp, "destroy", true, 5))
        {
            if(sscanf(params, "s[15]dS[64]", tmp, barriertype, tmp2)) return SendClientUsage(playerid, "/barrier destroy [id]");
            new bid = barriertype;
            if(bid < 1 || bid > MAX_BARRIERS) return SendClientError(playerid,"Invalid barrier id.");
            if(!Barrier[bid][bActive]) return SendClientError(playerid,"That barrier isn't deployed.");
            DestroyDynamicObject(Barrier[bid][bObject]);
            Delete3DTextLabel(Barrier[bid][bLabel]);
            Barrier[bid][bActive] = 0;
            SendClientMessage(playerid,-1,sprintf("Barrier ID %i has been destroyed.",bid));
        }
    else if(!strcmp(tmp, "list", true, 5))
    {
        for(new i = 1; i < MAX_BARRIERS; i++)
            {
                if(Barrier[i][bActive])  
                    {
                        SendClientMessage(playerid,-1,sprintf("Barrier ID %i | Note: %s",i,Barrier[i][Note]));
                    }
            }
    }
    else
        {
            SendClientUsage(playerid,"/barrier [ Deploy / Destroy / List]");
        }
    return 1;
}
/* ============================================================================= */
Not tested.
that doesn't even compile.

(rules state you have to test it before you can post it btw, nonetheless)

you cant use sscanf multiple times anyways.

Quote:
Originally Posted by Shinja
Посмотреть сообщение
PHP код:
new tmp15 ],tmp2,tmp3[200]; 
    if(
sscanf(params"s[15]is[200]"tmptmp2,tmp3)) 
when i use this, it shows this:

SendClientUsage(playerid,"/barrier [ Deploy / Destroy / List]");

everytime i try to do the command.
Reply
#8

Quote:
Originally Posted by justjamie
Посмотреть сообщение
(rules state you have to test it before you can post it btw, nonetheless)
Who told you that we should Test it before posting it?
Reply
#9

One small question, are you using the PAWN version of sscanf or the plugin?
Because I'm pretty sure "z" was removed, and sscanf gives warnings when you don't put the size.
Reply
#10

Quote:
Originally Posted by justjamie
Посмотреть сообщение
(rules state you have to test it before you can post it btw, nonetheless)
Seriously? If you provided enough details at first place (that's a rule too, by the way) I could have deleted the necessary lines of that horrible written code so I can test it myself.

Quote:
Originally Posted by justjamie
Посмотреть сообщение
you cant use sscanf multiple times anyways.
Here is A LOT of documentation about sscanf, go read some since it can be very handy later on.
https://sampforum.blast.hk/showthread.php?tid=570927


This works perfectly fine. TESTED.
Код:
/* ============================================================================ */ 
CMD:barrier(playerid, params[]) 
{ 
    new tmp[15], tmp2[64], barriertype; 
    if(sscanf(params, "s[15]DS[64]", tmp, barriertype, tmp2 ) ) return SendClientMessage(playerid, -1, "/barrier [ Deploy / Destroy / List]"); 
	if(!strcmp(tmp, "deploy", true)) 
	{  
		if(sscanf(params, "s[15]ds[64]", tmp, barriertype, tmp2)) return SendClientMessage(playerid, -1, "/barrier deploy [1/2/3/4] [Note]");  
		printf("%s %d %s", tmp, barriertype, tmp2);
	} 
	else if(!strcmp(tmp, "destroy", true)) 
	{ 
		if(sscanf(params, "s[15]d", tmp, barriertype)) return SendClientMessage(playerid, -1, "/barrier destroy [id]"); 
		printf("%s %d %s", tmp, barriertype, tmp2);
	} 
    else if(!strcmp(tmp, "list", true)) 
    { 
        print("list");
    } 
    else 
    { 
        SendClientMessage(playerid,-1,"/barrier [ Deploy / Destroy / List]"); 
    } 
    return 1; 
} 
/* ============================================================================= */
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)