PDA

View Full Version : Strlen and sscanf


Ahmed21
10/02/2017, 10:29 AM
Hello everyone,

So, I've made a command, to store text, here's the usage: /addhit [TEXT].

I want to store the text in "hInfo[hintid][hText]", and I want to add a condition "if the text length is more than 'HINT_LEN' then send an error to the player.

I've made it, but unfortunately it's completely bugged, help:

CMD:addhint(playerid, params[])
{
if(!IsPlayerAdmin(playerid)) return 0;

if(sscanf(params, "s[50]", params)) return SendUsageMSG(playerid, "/addhint [TEXT]");
if(strlen(params) >= HINT_LEN) return SendErrorMSG(playerid, "The text you entered is too long.");

new hintid = -1;
for(new i = 0; i < MAX_HINTS; i++)
{
if(hInfo[i][hID] == -1)
{
hintid = i;
break;
}
}
if(hintid == -1) return SendErrorMSG(playerid, "Amount of hints has reached the maximum.");

hInfo[hintid][hID] = hintid;
hInfo[hintid][hText] = params[50];
SendAdminMSG(playerid, "Hint has been added.");
return 1;
}

AmigaBlizzard
10/02/2017, 10:51 AM
First of all, using sscanf to extract the full text inside params and copy it back into params is quite useless.
Just delete that line as it won't do anything, except check if params is empty.

Replace the sscanf line by

if (strlen(params) == 0) return SendUsageMSG(playerid, "/addhint [TEXT]");


For storing the entered text into your enum, use format:

format(hInfo[hintid][hText], 50, params);

Using "hInfo[hintid][hText] = params[50]", you were only copying the 51st character into your hText variable (or the text starting from the 51st character till the end, which would never be anything because the length of your text is limited by 50 characters.

Ahmed21
10/02/2017, 11:10 AM
First of all, using sscanf to extract the full text inside params and copy it back into params is quite useless.
Just delete that line as it won't do anything, except check if params is empty.

Replace the sscanf line by

if (strlen(params) == 0) return SendUsageMSG(playerid, "/addhint [TEXT]");


For storing the entered text into your enum, use format:

format(hInfo[hintid][hText], 50, params);

Using "hInfo[hintid][hText] = params[50]", you were only copying the 51st character into your hText variable (or the text starting from the 51st character till the end, which would never be anything because the length of your text is limited by 50 characters.

Worked, thank you so much! :)