Strlen and sscanf
#1

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:

PHP Code:
CMD:addhint(playeridparams[])
{
    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 
0MAX_HINTSi++)
    {
        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;

Reply
#2

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
PHP Code:
if (strlen(params) == 0) return SendUsageMSG(playerid"/addhint [TEXT]"); 
For storing the entered text into your enum, use format:
PHP Code:
format(hInfo[hintid][hText], 50params); 
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.
Reply
#3

Quote:
Originally Posted by AmigaBlizzard
View Post
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
PHP Code:
if (strlen(params) == 0) return SendUsageMSG(playerid"/addhint [TEXT]"); 
For storing the entered text into your enum, use format:
PHP Code:
format(hInfo[hintid][hText], 50params); 
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!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)