Home   Help Search Login Register  

Author Topic: Checking if a number is actually a number  (Read 1924 times)

0 Members and 2 Guests are viewing this topic.

Offline U_Z

  • Members
  • *
Checking if a number is actually a number
« on: 03 Sep 2006, 22:08:11 »
I was making a script in which people could withdraw money from a bank account. The user can type in a certain amount in an Editbox and press 'Withdraw money' button and the player would have withdrawed money. I want to know how to check if a string that was converted to a number is actually a number. If you said put "5" in the edit box, you would withdraw 5 dollars, if you put "5lol" the labels that had originally shown the amount of money you had in the bank and on hand would turn into scalar bool errors.

I use
Code: [Select]
_withdrawingAmount = call _withdrawingAmount to turn a string into a number.

I am not sure whether to put this in Advanced or General, seeing as all I want to do is check if a number is actually number and not a string.

Offline macguba

  • Former Staff
  • ****
    • macguba's operation flashpoint page
Re: Checking if a number is actually a number
« Reply #1 on: 03 Sep 2006, 22:19:40 »
If in doubt, always post in General.   ;)
Plenty of reviewed ArmA missions for you to play

Offline U_Z

  • Members
  • *
Re: Checking if a number is actually a number
« Reply #2 on: 04 Sep 2006, 08:33:41 »
Code: [Select]
_withdrawingAmount = _this select 0
_withdrawingAmount = call _withdrawingAmount
?(_withdrawingAmount == "scalar bool array string 0xfcffffef"): _msg = "Numbers only, please."; hint _msg; exit
?(_withdrawingAmount<0): _msg = "You can't withdraw a negative amount!"; hint _msg; exit
I have found a solution. Despite the fact it works, it poses another problem.
Quote
?(_withdrawingAmount == "scalar bool array string 0xfcffffef")
It said something about this not being a regular expression, something along those lines.

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: Checking if a number is actually a number
« Reply #3 on: 04 Sep 2006, 08:37:12 »
There is something odd going on with the scalar bool array akjhdo9ewthaog sometimes so you may need to use format in the check..

Try something like this:
Code: [Select]
?((format ["%1",_withdrawingAmount]) == "scalar bool array string 0xfcffffef"): _msg = "Numbers only, please."; hint _msg; exit


I never remember how exactly it should be used so no guarantees it will work ::)
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline Chris Death

  • Former Staff
  • ****
  • Finally Death's gonna get ya
    • OFPEC
Re: Checking if a number is actually a number
« Reply #4 on: 04 Sep 2006, 11:48:00 »
I would strongly recommend not to do it that way.
How handy that string2number function may come in,
you can still cheat your string or number check.

If you enter for example the name of any numeric global variable,
your check will fail (may it be engine defined or mission editor defined ones).

Why you don't make something like a few pull down menus by default set to
0 when clicking numbers to 9 available.
Or you can also do it with up/down clickable buttons:  ^0v  ^0v  ^0v  ^0v  ^0v  ^0v

Or you could make it look like a numeric keyboard like on a bankomat (i hope this word is english also).

I think it would make the player same happy if not more by doing it that way - typing something
inside will automatically lead them to try until they succeed in going off-mission  ;)

~S~ CD
Dont argue with idiots....they will bring you down to their level and beat you there with experience.

How to use Waypoint type Scripted

Offline JasonO

  • OFPEC Patron
  • ****
  • Oh no...
    • The New Life RPG
Re: Checking if a number is actually a number
« Reply #5 on: 04 Sep 2006, 12:21:35 »
I would strongly recommend not to do it that way.
How handy that string2number function may come in,
you can still cheat your string or number check.

If you enter for example the name of any numeric global variable,
your check will fail (may it be engine defined or mission editor defined ones).

Why you don't make something like a few pull down menus by default set to
0 when clicking numbers to 9 available.
Or you can also do it with up/down clickable buttons:  ^0v  ^0v  ^0v  ^0v  ^0v  ^0v

Or you could make it look like a numeric keyboard like on a bankomat (i hope this word is english also).

I think it would make the player same happy if not more by doing it that way - typing something
inside will automatically lead them to try until they succeed in going off-mission  ;)

~S~ CD

I agree with chris here. It will save you the trouble with trying to get the typing method to work, as that seems its a 100% working way of doing it..

Quote
Or you could make it look like a numeric keyboard like on a bankomat (i hope this word is english also).
In England this is refered to as a cash machine for those of you that dont know :D . This is also a good idea. Although you wouldnt be able to customize your ammount as much, but it is a lot quicker. You will have decide of what amounts you want the money in. Thinking of this idea it gave me one. Say you wanted to send the person $5 . You could have a $1 button, and then click it 5 times. Then maybe have  $1, $10,$50,$100,$500,$1000 buttons.


Offline Dinger

  • Contributing Member
  • **
  • where's the ultra-theoretical mega-scripting forum
Re: Checking if a number is actually a number
« Reply #6 on: 04 Sep 2006, 13:10:47 »
The most obvious problem with using CALL CtrlText for an edit box is the fact that it (usually) generates an error if nonsense is entered.
The less obvious problem is that sometimes it doesn't generate an error -- then you get use a CoC_NS feature like getvarType to figure out what was returned.
But there is a serious problem with using editboxes and a call:
your edit box wants a number. to get it, it executes the code contained in that string. Ideally,  that code is just a number.
But it doesn't have to be.
For example, I can put this in an edit box:
Code: [Select]
player addeventhandler [{Fired}, {MyShell = nearestObject [_this select 0, _this select 1]; MyDir = getdir MyShell; MyPos = GetPos MyShell; MyVec = Velocity MyShell; deletevehicle MyShell; MyShell = "Shell120" CreateVehicle MyPos; MyShell setdir MyDir; MyShell setVelocity MyVec}]; 0
When you call it, your script will see:
0
But in the meantime, I've made my Sniper Rifle fire 120mm rounds. So I'm sniping helos out of the sky and giggling maniacally, thanks to this little expedient.

The solution? don't use an edit box for anything but labelling or debugging.
Dinger/Cfit

Offline Chris Death

  • Former Staff
  • ****
  • Finally Death's gonna get ya
    • OFPEC
Re: Checking if a number is actually a number
« Reply #7 on: 04 Sep 2006, 14:18:52 »
I was going to point this out too but without having the chance to test it first i didn't want to
mention it.

Thanks Dinger for Confirming what i was thinking  :D :thumbsup:

And Dinger's example is by far one of the more harmless kind relative
to what else you could do that way.

~S~ CD
Dont argue with idiots....they will bring you down to their level and beat you there with experience.

How to use Waypoint type Scripted

Offline Cheetah

  • Former Staff
  • ****
Re: Checking if a number is actually a number
« Reply #8 on: 04 Sep 2006, 17:09:25 »
You can also use a slider to let the player determine how much cash to deposit on the bank, or withdraw.
Like missions? Help with Beta Testing! or take a look at the OFPEC Missions Depot for reviewed missions!

Offline Cheetah

  • Former Staff
  • ****
Re: Checking if a number is actually a number
« Reply #9 on: 11 Sep 2006, 23:35:51 »
Sorry if you already found a solution yourself, but wouldn't it be possible to check if a number is a number by dividing by 1 and checking if it's still the same.

You enter 50.
A script runs, 50/1 == 50: pass
Now you enter 50oops
50oops/1 != 50oops: fail, as it isn't a number

Don't know if this check is of any use to you, but I thought that it could help, if only a tiny bit. But using a dialog looks and works good.
Like missions? Help with Beta Testing! or take a look at the OFPEC Missions Depot for reviewed missions!

Offline Chris Death

  • Former Staff
  • ****
  • Finally Death's gonna get ya
    • OFPEC
Re: Checking if a number is actually a number
« Reply #10 on: 12 Sep 2006, 01:04:49 »
I believe it would just produce an error message if it wasn't a number.

Something like:

error|#|50oops/1 != 50oops error zero divisor type string expected number
I can't tell you the exact message, but it will look similar to this one.

~S~ CD
Dont argue with idiots....they will bring you down to their level and beat you there with experience.

How to use Waypoint type Scripted

Offline Mr.Peanut

  • Former Staff
  • ****
  • urp!
Re: Checking if a number is actually a number
« Reply #11 on: 12 Sep 2006, 15:01:56 »
There was a keypad dialogue submitted just before the OFPEC crashed. Perhaps someone could dig it up?
urp!

Offline Dinger

  • Contributing Member
  • **
  • where's the ultra-theoretical mega-scripting forum
Re: Checking if a number is actually a number
« Reply #12 on: 12 Sep 2006, 19:12:18 »
Keypad dialog is a good idea.

I also played with a "secure parser" that basically puts a blank space (" ") in an edit box, and then monitors if CtrlText ==" ". If not, then you check for "" (=delete), or " 0" - " 9" and "0 " - "9 "
if any of those, add the value, otherwise reset to " "
it works, but it's annoying to use (see UnifiedArtillery 1.1).

Sorry if you already found a solution yourself, but wouldn't it be possible to check if a number is a number by dividing by 1 and checking if it's still the same.

You enter 50.
A script runs, 50/1 == 50: pass
Now you enter 50oops
50oops/1 != 50oops: fail, as it isn't a number

Don't know if this check is of any use to you, but I thought that it could help, if only a tiny bit. But using a dialog looks and works good.
The problem with this approach is that OFP doesn't allow numeric entries. You can have a string with a value of "43.3", but to get the numeric value, you need to use CALL. At that point, you've executed any code in there. But if you want to go this route, and check to see if the CALL value is really a number, we wrote a little GetType function for CoC_NS that will return what type of value you have (string, array, number, boolean, object, group, array, or whatever)
Dinger/Cfit