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
Have you tried VAL?()
like
DEFFN isnum(a$)=(VAL?(a$)=len(a$))
also you may want to use TRIM$() before or DECLOSE$()
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
you are right, something is wrong. looks like we have found a bug.
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
I fixed it and uploaded new source ta-ball. can you confirm that it works now?
Thanks for mentioning this issue!
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
Thank you! I included the program in the examples package under /calculation/.
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
yes thanks...