[Tutorial] Making Basic calculator commands.
#1

Hello,
Okay this is my 4th tutorial showing that how can you make a simple calculator commands.I know it sounds newbish but I made this for people who are new.Not tested so tell me if anything goes wrong.Or tell me if I have done something wrong.

Here is the full code with explanation:
pawn Код:
#include <a_samp>//you all know this.
#include <sscanf2>//including sscanf include in the script.
#if defined FILTERSCRIPT

#endif
new Answer;//the variable which would store the answer.
public OnPlayerCommandText(playerid, cmdtext[])
{
    if (strcmp("/plus", cmdtext, true, 10) == 0)
    {
        new str[128],N1,N2;//these are the variables that will store the text of the string,the first number entered and the second.
        if(sscanf(cmdtext, "ii", N1, N2)) return SendClientMessage(playerid, -1, "Usage: /plus (1st number) (2nd number)");//this will send the player an error message if he enters invalid information or in a wrong format.
        format(str, sizeof(str), "The answer to %i + %i is %i", N1, N2, Answer);//this will format the answer and all the calculations and store it in the variable str.
        SendClientMessage(playerid,-1,str);//now this will send the information stored in the variable str to the player.
        return 1;
    }
    else if (strcmp("/minus", cmdtext, true, 10) == 0)
    {
        new str[128],N1,N2;//these are the variables that will store the text of the string,the first number entered and the second.
        if(sscanf(cmdtext, "ii", N1, N2)) return SendClientMessage(playerid, -1, "Usage: /minus (1st number) (2nd number)");//this will send the player an error message if he enters invalid information or in a wrong format.
        format(str, sizeof(str), "The answer to %i - %i is %i", N1, N2, Answer);//this will format the answer and all the calculations and store it in the variable str.
        SendClientMessage(playerid,-1,str);//now this will send the information stored in the variable str to the player.
        return 1;
    }
    else if (strcmp("/multiplay", cmdtext, true, 10) == 0)
    {
        new str[128],N1,N2;//these are the variables that will store the text of the string,the first number entered and the second.
        if(sscanf(cmdtext, "ii", N1, N2)) return SendClientMessage(playerid, -1, "Usage: /multiplay (1st number) (2nd number)");//this will send the player an error message if he enters invalid information or in a wrong format.
        format(str, sizeof(str), "The answer to %i x %i is %i", N1, N2, Answer);//this will format the answer and all the calculations and store it in the variable str.
        SendClientMessage(playerid,-1,str);//now this will send the information stored in the variable str to the player.
        return 1;
    }
    return 0;
}
replies appreciated.
Reply
#2

slow way, and it should be float's. kinda useless this thread.
Reply
#3

I know this is a slow way and cause the newbies would not use zcmd and things the first day,And I said that this was for newbies not for pro's like you ,thanks for the reply.
Reply
#4

I don't think this will work
Reply
#5

I have not tested it,just giving people idea how to make it.Thanks for the feed back.
Reply
#6

Quote:
Originally Posted by TaLhA XIV
Посмотреть сообщение
I have not tested it,just giving people idea how to make it.Thanks for the feed back.
Please test it... Seriously, test it!
Reply
#7

bugged one:
pawn Код:
#include <a_samp>//you all know this.
#include <sscanf2>//including sscanf include in the script.
#if defined FILTERSCRIPT

#endif
new Answer;//the variable which would store the answer.
public OnPlayerCommandText(playerid, cmdtext[])
{
    if (strcmp("/plus", cmdtext, true, 10) == 0)
    {
        new str[128],N1,N2;//these are the variables that will store the text of the string,the first number entered and the second.
        if(sscanf(cmdtext, "ii", N1, N2)) return SendClientMessage(playerid, -1, "Usage: /plus (1st number) (2nd number)");//this will send the player an error message if he enters invalid information or in a wrong format.
        format(str, sizeof(str), "The answer to %i + %i is %i", N1, N2, Answer);//this will format the answer and all the calculations and store it in the variable str.
        SendClientMessage(playerid,-1,str);//now this will send the information stored in the variable str to the player.
        return 1;
    }
    else if (strcmp("/minus", cmdtext, true, 10) == 0)
    {
        new str[128],N1,N2;//these are the variables that will store the text of the string,the first number entered and the second.
        if(sscanf(cmdtext, "ii", N1, N2)) return SendClientMessage(playerid, -1, "Usage: /minus (1st number) (2nd number)");//this will send the player an error message if he enters invalid information or in a wrong format.
        format(str, sizeof(str), "The answer to %i - %i is %i", N1, N2, Answer);//this will format the answer and all the calculations and store it in the variable str.
        SendClientMessage(playerid,-1,str);//now this will send the information stored in the variable str to the player.
        return 1;
    }
    else if (strcmp("/multiplay", cmdtext, true, 10) == 0)
    {
        new str[128],N1,N2;//these are the variables that will store the text of the string,the first number entered and the second.
        if(sscanf(cmdtext, "ii", N1, N2)) return SendClientMessage(playerid, -1, "Usage: /multiplay (1st number) (2nd number)");//this will send the player an error message if he enters invalid information or in a wrong format.
        format(str, sizeof(str), "The answer to %i x %i is %i", N1, N2, Answer);//this will format the answer and all the calculations and store it in the variable str.
        SendClientMessage(playerid,-1,str);//now this will send the information stored in the variable str to the player.
        return 1;
    }
    return 0;
}
correct & better one:

pawn Код:
#include <a_samp>//you all know this.
#include <sscanf2>//including sscanf include in the script.
#if defined FILTERSCRIPT

#endif
public OnPlayerCommandText(playerid, cmdtext[])
{
    if (strcmp("/plus", cmdtext, true, 10) == 0)
    {
        new str[128],N1,N2;//these are the variables that will store the text of the string,the first number entered and the second.
        if(sscanf(cmdtext, "ii", N1, N2)) return SendClientMessage(playerid, -1, "Usage: /plus (1st number) (2nd number)");//this will send the player an error message if he enters invalid information or in a wrong format.
        format(str, sizeof(str), "The answer to %i + %i is %i", N1, N2, N1 + N2);//this will format the answer and all the calculations and store it in the variable str.
        SendClientMessage(playerid,-1,str);//now this will send the information stored in the variable str to the player.
        return 1;
    }
    else if (strcmp("/minus", cmdtext, true, 10) == 0)
    {
        new str[128],N1,N2;//these are the variables that will store the text of the string,the first number entered and the second.
        if(sscanf(cmdtext, "ii", N1, N2)) return SendClientMessage(playerid, -1, "Usage: /minus (1st number) (2nd number)");//this will send the player an error message if he enters invalid information or in a wrong format.
        format(str, sizeof(str), "The answer to %i - %i is %i", N1, N2, N1 - N2);//this will format the answer and all the calculations and store it in the variable str.
        SendClientMessage(playerid,-1,str);//now this will send the information stored in the variable str to the player.
        return 1;
    }
    else if (strcmp("/multiplay", cmdtext, true, 10) == 0)
    {
        new str[128],N1,N2;//these are the variables that will store the text of the string,the first number entered and the second.
        if(sscanf(cmdtext, "ii", N1, N2)) return SendClientMessage(playerid, -1, "Usage: /multiplay (1st number) (2nd number)");//this will send the player an error message if he enters invalid information or in a wrong format.
        format(str, sizeof(str), "The answer to %i x %i is %i", N1, N2, N1 * N2);//this will format the answer and all the calculations and store it in the variable str.
        SendClientMessage(playerid,-1,str);//now this will send the information stored in the variable str to the player.
        return 1;
    }
    return 0;
}
PS: it should be /multiply not /multiplay
Reply
#8

Glad some one test it,ThAnKs!
Reply
#9

Why are you putting the no. of characters of the command 10 though it is 9?
pawn Код:
strcmp("/multiplay", cmdtext, true, [B]10[/B]) == 0) }
It should be
pawn Код:
strcmp("/multiplay", cmdtext, true, [B]9[/B]) == 0) }
If you don't know what the fourth parameter stands for, it is the no. of characters in your command.
Reply
#10

pawn Код:
#include <a_samp>
#include <sscanf2>

#define CALC_ADD        1
#define CALC_MINUS      2
#define CALC_MULTIPLY   3
#define CALC_DIVIDE     4

stock calculate(playerid, number1, number2, method) {
    new outcome, string[128];
    switch(method) {
        case CALC_ADD: format(string, 128, "%d + %d = %d", number1, number2, number1 + number2), SendClientMessage(playerid, -1, string);
        case CALC_MINUS: format(string, 128, "%d - %d = %d", number1, number2, number1 - number2), SendClientMessage(playerid, -1, string);
        case CALC_MULTIPLY: format(string, 128, "%d * %d = %d", number1, number2, number1 * number2), SendClientMessage(playerid, -1, string);
        case CALC_DIVIDE: format(string, 128, "%d / %d = %d", number1, number2, number1 / number2), SendClientMessage(playerid, -1, string);
    }
}

public OnPlayerCommandText(playerid, cmdtext[])
{
    if(strcmp("/calc", cmdtext, true, 5) == 0 || strcmp("/calculate", true, 10) == 0) {
        new str[128], func[2], n1, n2, type;
        if(sscanf(cmdtext, "iis[2]", n1, n2, func)) return SendClientMessage(playerid, -1, "Usage: /calc(ulate) (1st number) (2nd number) (function: + - / *)");
        if(!sscanf(func, "+", true, 1)) { type = CALC_ADD; } else if(!sscanf(func, "-", true, 1)) { type = CALC_MINUS; } else if(!sscanf(func, "*", true, 1)) { type = CALC_MULTIPLY; } else if(!sscanf(func, "/", true, 1)) { type = CALC_DIVIDE; }
        calculate(playerid, n1, n2, type);
    }
    return 0;
}
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)