From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 1 Nov 92 16:55:51 GMT From: seas.gwu.edu!mfeldman@uunet.uu.net (Michael Feldman) Subject: Re: DATA_ERROR exception problem Message-ID: <1992Nov1.165551.406@seas.gwu.edu> List-Id: In article dww@inf.fu-berlin.de writes: >[Now that we have the umlauts sorted out, the next problem!] > >I want to write a compeletly safe input procedure for integers: >I GET an integer, and if DATA_ERROR is raised, I want to loop >around until I get a decent answer. Very common problem. > >My problem is that DATA_ERROR is raised on two different conditions: >an integer being illegal, i.e. out of bounds, or an illegal >character being encountered (such as when I enter "1.0" or "z"). >The problem ist that in the former case, the input buffer has been >emptied (all digits read); in the latter, the illegal character >(and all following characters) remain in the input buffer. >How can I differentiate the two cases, so that I can >GET_LINE (input_garbage, input_garbage_size) and reprompt for input? See below. > >On Meridian's PC Ada I get an endless loop if I don't remove the >garbage, and have to enter extra garbage if the problem was a bound >error (!!). Is there any other way to do this besides reading in the >input as a string and patching together an integer myself :-( ? This is not a Meridian problem but the way Text_IO is defined in the LRM. "The character that causes input to cease remains available for subsequent input." The most "industrial-strength" solution is indeed to read a line into a string. You don't quite have to "patch together" an integer character- by-character, because you can always use the Integer_IO Get that Get's from the string instead of the input buffer. If the operation fails, you can parse the string a bit to see why it failed. Check your LRM or Ada book for details on how these Get operations work. Many people do not realize that they are there! This is actually quite similar to problems with C input; many C books recommend using a similar solution technique, making use of sscanf instead of scanf. So Text_IO doesn't really solve a rather classical interactive-input problem. On the other hand, at least it's no worse :-) Are your students first-time programmers - first-year students, whatever - or just beginners to Ada? Good luck! Let's hear how you make out! Mike Feldman ------------------------------------------------------------------------ Michael B. Feldman co-chair, SIGAda Education Committee Professor, Dept. of Electrical Engineering and Computer Science School of Engineering and Applied Science The George Washington University Washington, DC 20052 USA (202) 994-5253 (voice) (202) 994-5296 (fax) mfeldman@seas.gwu.edu (Internet) "Americans wants the fruits of patience -- and they want them now." ------------------------------------------------------------------------