comp.lang.ada
 help / color / mirror / Atom feed
From: seas.gwu.edu!mfeldman@uunet.uu.net  (Michael Feldman)
Subject: Re: DATA_ERROR exception problem
Date: 1 Nov 92 16:55:51 GMT	[thread overview]
Message-ID: <1992Nov1.165551.406@seas.gwu.edu> (raw)

In article <X7D8JTH@math.fu-berlin.de> 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."
------------------------------------------------------------------------

             reply	other threads:[~1992-11-01 16:55 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1992-11-01 16:55 Michael Feldman [this message]
  -- strict thread matches above, loose matches on Subject: below --
1992-11-06  3:54 DATA_ERROR exception problem Michael Feldman
1992-10-31  6:34 munnari.oz.au!yoyo.aarnet.edu.au!news.adelaide.edu.au!cs.adelaide.edu.au!
1992-10-29 21:26 David Erickson
1992-10-29 10:44 bloom-picayune.mit.edu!snorkelwacker.mit.edu!ira.uka.de!ira.uka.de!math.f
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox