Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 16:59 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Check if input data is integer or not (MMX V5.0504)

Author Message
AussieWombat
Newbie

Joined: 04/05/2018
Location: Australia
Posts: 21
Posted: 02:20am 07 Feb 2021
Copy link to clipboard 
Print this post

Hi all,

I have an input statement, that has to be an integer.
If I type a word into the input, it accepts it.

I was going to use an if then statement to check if it is (NAN),

I am using


fontwidth:
Input "Width of Font? ",fwidth%
IF (fwidth% =0) then
print "not a number"
goto fontwidth
endif


but if they enter zero, that stuffs that up . Or should I add to the input statement.
Input "Width of Font?(1-255) ",fwidth%


regards
AussieWombat
 
Chopperp

Guru

Joined: 03/01/2018
Location: Australia
Posts: 1097
Posted: 03:07am 07 Feb 2021
Copy link to clipboard 
Print this post

I did a quick prog. Needs refining

fwidth% = 0
Do While fwidth%  = 0
 Input "Width of string ", fwidth%
 Print fwidth%
Loop


Brian
ChopperP
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3292
Posted: 10:40am 07 Feb 2021
Copy link to clipboard 
Print this post

Brian's code is good but three extra points:

You use "IF (fwidth% =0) then" without an ENDIF.  This is why entering zero causes an error.  As an aside, when you are asking for help on this forum it would be worth giving a clear description and listing the full error message rather than just saying "stuffs that up".

If the user just hits Enter at the prompt fwidth% will be unchanged.  If this is not what you want it would be worth initialising fwidth% to some default value (ie, 0 as in Brian's code).

You do not need brackets around conditional tests (ie, (fwidth% =0)) but also they don't hurt.  I'm guessing that you come from a C background.

Geoff
Geoff Graham - http://geoffg.net
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 02:18pm 07 Feb 2021
Copy link to clipboard 
Print this post

Assuming you don't want anything but digits i.e. no decimals...


fontwidth:
Do
   Input "Width of Font? ",a$
While Len(a$)<1

For n=1 to len(a$)
   if instr(" 0123456789",Mid(a$,n,1)<2 then
       print "not a number"
       goto fontwidth
   endif
next

fwidth%=val(a$)



I always like to grab data as strings and then process into whatever form I need. Minimises errors because of data mis-matches and you maintain control of everything in the string. As an aside, I avoid Input - you drop into the guts of MMBasic while it is waiting for the enter key which is "blocking" i.e. it stops your code running (interrupts, timers etc)

One of my pet likes is to convert VB functions into MMBasic. I never did IsNumeric() as I never really saw much call for it, but now...  With it, you could simply have:


fontwidth:

Input "Width of Font? ",a$

If IsNumeric(a$) Then
   fwidth%=val(a$)
Else
   print "not a number"
   goto fontwidth
EndIf





Any IsNumeric() function would be pretty much the same as the first bit of code above, but with some sanity checking and switches to allow decimals (one) etc.

Happy to do it for you and add it to the library if you would like that.

You might also like to look at this Constrain() function
Edited 2021-02-08 00:32 by CaptainBoing
 
Schlowski
Newbie

Joined: 26/03/2014
Location: Germany
Posts: 29
Posted: 02:37pm 07 Feb 2021
Copy link to clipboard 
Print this post

Isn't jumping with GOTO out of a FOR-NEXT loop without proper ending the loop critical?

I would assume that this leaves some sort of unresolved FOR-information on some sort of stack or so...
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 02:42pm 07 Feb 2021
Copy link to clipboard 
Print this post

It is bad form, yes I agree and GoTo is seen as structured programming's arch-enemy. I wanted to make the code as "look-a-like" to the original so as not to stray too much and take away from the original query - hence following up with an example using a nice function - structure restored.

Regardless, MMBasic (at least on MM2 so I suspect all other platforms are the same as the core language hasn't been changed) is quite happy. True some languages use the stack for loops and this would as you suspect blow up memory, but not MMBasic (same with Do/Loop and Select/End Select). Internally they seem to act almost as if they are "aliases" for If/GoTo pairs, mimicking what CPUs do in the binary with test and Jump.

Try this bit of code below, just runs happily forever



Fred:

For n=1 To 1000
GoTo Fred
Next


Edited 2021-02-08 01:23 by CaptainBoing
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 07:42pm 07 Feb 2021
Copy link to clipboard 
Print this post

For this problem, I prefer to use INKEY$ and prevent non-numeric characters from getting entered.

This is written for the CMM2 but shouldn't need much change for any MMBasic platform.
 ' text input routines
 ' option console serial ' to stop messing up the display
 CONST FALSE = 0
 CONST TRUE = NOT false
 'dim n$(10)
 'dim config$(10,3)
 DIM INTEGER tHt = MM.INFO(FONTHEIGHT), tWth = MM.INFO(FONTWIDTH)
 DIM FLOAT num
 'dim integer k
 CLS
 
 num = GetNum(10,10,"42")
 PRINT
 PRINT num
 
 ' Gets valid numeric input from user
 ' Parameters:
 ' Row, Col - location to echo input
 ' Initial$ - default value
FUNCTION GetNum(Col AS INTEGER, Row AS INTEGER,initial$)
 LOCAL result$, Kbd$
 LOCAL INTEGER Done
 Result$ = initial$
 Done = FALSE
 DO WHILE INKEY$ <> "": LOOP 'Clear keyboard buffer
 
 DO WHILE NOT Done
   TEXT col*tWth,Row*tHt,Result$+CHR$(95)+ "    ",lt,1,1
   DO
     Kbd$ = INKEY$
   LOOP UNTIL Kbd$ <> ""
   
   SELECT CASE Kbd$
     CASE CHR$(27)
       result$ = initial$
       done = true
     CASE "0","1","2","3","4","5","6","7","8", "9"
       Result$ = Result$ + Kbd$
     CASE "."
       IF INSTR(Result$, ".") = 0 THEN
         Result$ = Result$ + Kbd$
       ELSE
         PLAY TONE 440,440,100
       END IF
     CASE CHR$(13),CHR$(9)
       Done = TRUE
     CASE CHR$(8)
       IF LEN(Result$) > 0 THEN
         Result$ = LEFT$(Result$, LEN(Result$) - 1)
       END IF
     CASE ELSE
       IF LEN(Kbd$) > 0 THEN
         PLAY TONE 440,440,100
       END IF
   END SELECT
 LOOP
 
 TEXT col*tWth,Row*tHt,Result$+ "    ",lt,1,1
 GetNum = VAL(Result$)
END FUNCTION
 
END


Jim
VK7JH
MMedit
 
AussieWombat
Newbie

Joined: 04/05/2018
Location: Australia
Posts: 21
Posted: 06:58am 09 Feb 2021
Copy link to clipboard 
Print this post

Thanks all for the replies,

I was just going through some old programs I had written for the Maximite and CMM1, and trying to get them to work in the MMX V5.0504

They will probably never see the light of day, but I thought it would be a good exercise.

I like the approach of TassyJim, and use the inkey$, as then interupts would not be hindered.

It was for an old font creation program, but now we can all use TassyJims FontTweak program to create fonts.

Aussiewombat
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025