Menu

#4 input n vs input n$

v1.0 (example)
closed
None
5
2014-03-20
2014-03-19
Nick Warne
No

Is there a quick way to determine if a requested input is a number rather than a string: exapmle:

print "Please input a number"
input s
print s

If it is a number (123, say) then all well and good. But if user inputs "abc"

print s will produce '0', so of course there is no way to check this.

I have got around it at the moment using this rather unsavoury function:

print "input"
input s$
res=@isanum(n,s$)
if res=1
print s;" ";s*s
else
print "Not a number!"
endif
goto again

function isanum(n,s$)
l=len(n$)
for a = 1 to l
'check for period, i.e. '3.141592
if asc(mid$(s$,a,1)) = 46
break
endif
'check if all digits
if asc(mid$(s$,a,1)) < 48 or asc(mid$(s$,a,1)) > 57
return 0,s$
endif
next a
s=val(s$)
return 1,s
endfunc

Thanks,
Nick

Discussion

  • Markus Hoffmann

    Markus Hoffmann - 2014-03-19

    Have you tried VAL?()

    like
    DEFFN isnum(a$)=(VAL?(a$)=len(a$))

    also you may want to use TRIM$() before or DECLOSE$()

     
  • Nick Warne

    Nick Warne - 2014-03-19

    Ahh... thank you. I looked at val?() but couldn't get it to work:

    if val?(s$) <> len(s$)

    produces:

    ERROR at line 5: Array not dimensioned: VAL?()

    I see now I have to assign it to a var$

    Thanks for help,

    Nick

     
  • Markus Hoffmann

    Markus Hoffmann - 2014-03-19

    you are right, something is wrong. looks like we have found a bug.

     
  • Nick Warne

    Nick Warne - 2014-03-19

    Yes, sorry I though it was working... due to calling the DEFFN function the same name as the full function... so the code was still calling the old function (and ignored the DEFFN) so it worked...

    Nick

     
  • Markus Hoffmann

    Markus Hoffmann - 2014-03-19

    I fixed it and uploaded new source ta-ball. can you confirm that it works now?

    Thanks for mentioning this issue!

     
  • Nick Warne

    Nick Warne - 2014-03-19

    Yep, all works great!

    Many thanks for creating X11Basic - I used GFA basic many years ago and love it's ability to do almost anything.

    Great stuff.

    BTW, I have attached a small file if you wish to include it in 'examples' - it includes the inline DEFFN to test if input is a number or not - also it demonstrates how bloody fast the produced binaries are!

    Again, many thanks for this great bit of kit!

    Nick

     
  • Markus Hoffmann

    Markus Hoffmann - 2014-03-19

    Thank you! I included the program in the examples package under /calculation/.

     
  • Nick Warne

    Nick Warne - 2014-03-19

    OK, here is another little program for producing primes if you wish to use it in /calculation/

    I used the alternative val?(n$)<>len(n$) here.

    Nick

     
  • Markus Hoffmann

    Markus Hoffmann - 2014-03-20
    • status: open --> closed
     
  • Markus Hoffmann

    Markus Hoffmann - 2014-03-20

    yes thanks...

     

Log in to post a comment.

Auth0 Logo