Checking if a variable is a number
#1

how can i do a check to see if a variable is all number for example

Код:
 if(inputtext = is a number);
{
}
else return SendClientMessage(playerid, colour, you can only enter numbers sorry);
Reply
#2

IsNumeric:

pawn Код:
stock IsNumeric(const string[])
{
        for (new i = 0, j = strlen(string); i < j; i++)
        {
                if (string[i] > '9' || string[i] < '0') return 0;
        }
        return 1;
}
pawn Код:
if(IsNumeric(inputtext))
{
//its a number!
}
else return 1; //not a number.
Reply
#3

Use this stock:
pawn Код:
stock isNumeric(const string[])
{
 new length=strlen(string);
 if (length==0) return false;
 for (new i = 0; i < length; i++)
  {
   if (
      (string[i] > '9' || string[i] < '0' && string[i]!='-' && string[i]!='+') // Not a number,'+' or '-'
       || (string[i]=='-' && i!=0)                       // A '-' but not at first.
       || (string[i]=='+' && i!=0)                       // A '+' but not at first.
     ) return false;
  }
 if (length==1 && (string[0]=='-' || string[0]=='+')) return false;
 return true;
}
Or better, I suggest using sscanf to check for the formats.

pawn Код:
//IsNumeric Example:
new value[32] = "5748";
if( IsNumeric(value) ) return print( "Yes" );
Reply
#4

i now get this error:
Код:
error 033: array must be indexed (variable "inputtext")
from this line:
Код:
if(inputtext == PlayerInfo[playerid][Bankdeposit] || inputtext < PlayerInfo[playerid][Bankdeposit])
Reply
#5

After IsNumeric check, make the line this way:

pawn Код:
if(strval(inputtext) == PlayerInfo[playerid][Bankdeposit] || strval(inputtext) < PlayerInfo[playerid][Bankdeposit])
Why, because inputtext is STILL a string, right?

strcmp to compare strings, == for ints.
Reply
#6

compiled fine =P thanks
Reply
#7

Use sscanf bro.
pawn Код:
new numeric;
if(sscanf(params, "i", numeric))
{
      return 1;
}
else
{
      // your command
}
Reply
#8

so how do i do this:

Код:
GivePlayerMoney(playerid, -strval(inputtext));
i want the strval(inputtext); to take the amount in inputtext away
Reply
#9

Why you don't try "-12345" for example in inputtext?
(with minus)
Reply
#10

because then the input the player puts in wont be there and it will just take -12345 off .....
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)