Re: Tutorial of sorts
- Posted by Irv Mullins <irv at ELLIJAY.COM> Oct 24, 1998
- 522 views
On Sat, 24 Oct 1998 05:42:03 -0400, Ad Rienks <Ad_Rienks at COMPUSERVE.COM> wrote: >>>assignment 1.1A perhaps? >Allright, here's my attempt. The function is 12 lines. You can deduct me 2 >points. > >-- validate.e >-- a simple function to get data input from the user, and check for bounds > >include get.e > >function validate(sequence prompt, sequence bounds) >object result > while 1 do > puts(1, prompt & ' ') > result = gets(0) > result = value(result[1..length(result)-1]) > if result[1] = GET_SUCCESS then > if result[2] >= bounds[1] and result[2] <= bounds[2] then > return result[2] > end if > end if > printf(1, "\nError! Range = %.2f to %.2f\n", bounds) > end while >end function > >--test >? validate("Hours worked:", {1, 80}) >-- end of Euphoria code > >Ad Now, here's a chance to illustrate something about programming: The above routine is specialized, in that it always takes input from the keyboard. We have to write another routine to validate input entered on the command line, and perhaps another for input from a disk file. No fun, amd more code = more bugs So, I suggest we write: 1. a validate routine that takes passed input and validates it, regardless of whether the input comes from the keyboard, the command line, or a disk file. Q. What are the appropriate responses to the following failures? 1. Keyboard input invalid: 2. Command line input invalid: 3. Disk input invalid: 2. an input routine that prints a prompt, calls validate, and returns the keyed input, or retries if the validity check fails. Q. How best to indicate the reason for the failure, so the user can try again? (This should be something more meaningful than BASIC's silly RE-DO FROM START message!) We'll use these two routines in almost every program from now on, so it's worth spending a little time writing various versions. Apologies if this gets posted twice, my ISP is changing their mail server this weekend, and all kinds of strange things are happening. Irv