Page 1 of 2
SuperBASIC function for type validation
Posted: Thu Jul 09, 2020 7:27 am
by grafvonb
I can't find it

. How to check if a non-numeric of empty data has been entered in a numeric field (or vice versa) using SuperBASIC?
Re: SuperBASIC function for type validation
Posted: Thu Jul 09, 2020 8:58 am
by Artificer
Hi,
Are you looking for a function/procedure in Superbasic to ensure only numeric input? If so there are a few examples in QL World Magazine.
In QLWorld 1986 November 1986 page 36 there is a Superbasic function. The magazine can be downloaded from Dilwyn Jones site here
http://www.dilwyn.me.uk/mags/qluserworl ... 986-11.pdf
There was also a calculator project later on perhaps in 1988.
Cheers
Re: SuperBASIC function for type validation
Posted: Thu Jul 09, 2020 10:35 am
by tofro
There are quite some ways to skin that cat:
The
S*Basic reference manual has another nice example of a "safe input" routine in its INPUT section that doesn't need any Toolkit.
With some "modern" toolkits/compilers you can make this significantly simpler.
WHEN ERROR: Catch the "bad parameter" error on coercion and repeat input
DIY Toolkit has some functions (CHECKF and CHECK%) that test whether a string can safely be converted to a number.
Turbo Toolkit has functions EDITF and EDIT%, which will reject anything that is not a proper number.
Re: SuperBASIC function for type validation
Posted: Thu Jul 09, 2020 11:03 am
by NormanDunbar
Code: Select all
1000 WHEN ERRor
1010 PRINT "That was not a number!"
1020 CONTINUE
1030 END WHEN
1040 :
1050 :
2000 REPeat loop
2010 INPUT "Please enter a number: "; num$
2020 num = num$
2030 PRINT "Your number was: " & num
2040 END REPeat loop
The above sort of works. Empty strings or anything not starting with a digit cause the error handler to fire up and tell you off. It converts anything that starts with a digit to a number
up until the first non-digit character. "123x" will return the value 123 and not cause an error.
Number formats such as "1e6" don't fire the error handler, but return 1e6 as the value. Floating point numbers are happily accepted as well.
HTH
Cheers,
Norm.
Re: SuperBASIC function for type validation
Posted: Thu Jul 09, 2020 11:05 am
by NormanDunbar
Beaten, once again, by tofro.

Re: SuperBASIC function for type validation
Posted: Thu Jul 09, 2020 11:07 am
by tofro
NormanDunbar wrote:Beaten, once again, by tofro.

No worries, Norm, I'll hold back next time
At least you've presented an example that works in SBASIC - I haven't.
Tobias
Re: SuperBASIC function for type validation
Posted: Thu Jul 09, 2020 11:17 am
by NormanDunbar
Nah, don't hold back, I have been known to be wrong! Just ask my wife!
Cheers,
Norm.
Re: SuperBASIC function for type validation
Posted: Thu Jul 09, 2020 11:21 am
by grafvonb
Many thanks guys, I have now a lot of stuff to check!
Re: SuperBASIC function for type validation
Posted: Thu Jul 09, 2020 12:48 pm
by pjw
Ive never been fond of WHEN ERRor. I just use it as a last resort. Perhaps this is because of all the problems the various implementation had in their early incarnations. But even now there are tricky issues to beware of. Viz:
Code: Select all
1000 WHEN ERRor
1010 PRINT "That was not a number!"
1020 CONTINUE
1030 END WHEN
1040 :
1050 :
2000 REPeat loop
2010 INPUT "Please enter a number: "; num$
2015 IF num$ = '': EXIT loop
2020 num = num$
2030 PRINT "Your number was: " & num
2040 END REPeat loop
2050 :
2060 num = 5/0: REMark Just a trivial example
The problem being demonstrated is that the next error encountered will (probably unintentionally) trip the same WHEN ERRor clause. Ones needs to remember to turn WHEN ERRor off:
Code: Select all
1000 WHEN ERRor
1010 PRINT "That was not a number!"
1020 CONTINUE
1030 END WHEN
1040 :
1050 :
2000 REPeat loop
2010 INPUT "Please enter a number: "; num$
2015 IF num$ = '': EXIT loop
2020 num = num$
2030 PRINT "Your number was: " & num
2040 END REPeat loop
2050 :
2052 :
2055 WHEN ERRor
2057 END WHEN: REMark WHEN ERR turned off
2058 :
2060 num = 5/0
Re: SuperBASIC function for type validation
Posted: Thu Jul 09, 2020 1:11 pm
by NormanDunbar
Hi Per,
good point, well made. Thanks.
Cheers,
Norm.