SuperBASIC function for type validation

Anything QL Software or Programming Related.
User avatar
grafvonb
Bent Pin Expansion Port
Posts: 80
Joined: Sat Jan 05, 2019 7:54 am
Location: Frankfurt/Germany

SuperBASIC function for type validation

Post 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?


Loving QL & ZX Spectrum & Amiga...
User avatar
Artificer
Brittle Membrane
Posts: 120
Joined: Fri Nov 24, 2017 8:43 am

Re: SuperBASIC function for type validation

Post 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


User avatar
tofro
Font of All Knowledge
Posts: 2685
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: SuperBASIC function for type validation

Post 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.


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
User avatar
NormanDunbar
Forum Moderator
Posts: 2251
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: SuperBASIC function for type validation

Post 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.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
User avatar
NormanDunbar
Forum Moderator
Posts: 2251
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: SuperBASIC function for type validation

Post by NormanDunbar »

Beaten, once again, by tofro. :(


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
User avatar
tofro
Font of All Knowledge
Posts: 2685
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: SuperBASIC function for type validation

Post 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


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
User avatar
NormanDunbar
Forum Moderator
Posts: 2251
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: SuperBASIC function for type validation

Post by NormanDunbar »

Nah, don't hold back, I have been known to be wrong! Just ask my wife!

Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
User avatar
grafvonb
Bent Pin Expansion Port
Posts: 80
Joined: Sat Jan 05, 2019 7:54 am
Location: Frankfurt/Germany

Re: SuperBASIC function for type validation

Post by grafvonb »

Many thanks guys, I have now a lot of stuff to check!


Loving QL & ZX Spectrum & Amiga...
User avatar
pjw
QL Wafer Drive
Posts: 1286
Joined: Fri Jul 11, 2014 8:44 am
Location: Norway
Contact:

Re: SuperBASIC function for type validation

Post 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


Per
dont be happy. worry
- ?
User avatar
NormanDunbar
Forum Moderator
Posts: 2251
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: SuperBASIC function for type validation

Post by NormanDunbar »

Hi Per,

good point, well made. Thanks.


Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
Post Reply