1. Re: Input Numbers
- Posted by Steve Elder <SteveElde at AOL.COM> Aug 21, 1997
- 613 views
> I find that for a project I am working on, I need some > "bulletproof" routines for numeric input which allow for "full" > input editing (at least supporting destructive backspace). At a > minimum, I need to be able to optionally restrict input to > integral values; best would be if I could both restrict the type > (integer vs. float/atom) and the range (lowest and highest > allowed values, allowing for open-ended ranges). I would be duly > grateful if someone could point me in an appropriate direction. > > Jeff Zeitlin > jeff.zeitlin at earth.execnet.com Here's something that should help. Steve Elder --------- code begins here include get.e include graphics.e global function input(sequence prompt) -- print a message and read in a number -- returns the number, that can either be an integer or an atom(float) sequence input, curpos input = {1, 0} -- this initializes the variable input with -- values that are *not* valid puts(1, prompt) -- put message on screen while input[1] do -- repeat until valid number curpos = get_position() -- read current cursor position input = get(0) -- read from the keyboard position(curpos[1], curpos[2]) -- back to position if not valid end while puts(1, '\n') -- to start on a new line when back from function return input[2] -- the second element of input is returned -- and should contain a valid number end function -- usage: object a -- Restricts input to integer. Otherwise declare a as an --atom or object a = input("enter a number: ") -- an example of restricting input to 10 or less if a > 10 then puts(1,"your number must be 10 or less") else ? a end if