comp.lang.ada
 help / color / mirror / Atom feed
From: John McCormick <mccormick@cs.uni.edu>
Subject: Data_Error and Enumeration_IO
Date: Thu, 12 Jan 2012 14:41:07 -0800 (PST)
Date: 2012-01-12T14:41:07-08:00	[thread overview]
Message-ID: <3f3d626a-1b8c-49af-aa85-9e586029a817@z12g2000yqm.googlegroups.com> (raw)

I need some assistance in understanding the consumption of input
characters when a Data_Error is raised during the input of an
enumeration value.  My experiments show that there is a different
behavior depending on whether the illegal input consists of alphabetic
characters or digits.

Here is a short example to illustrate my ingorance.  The following
code fragment reads in an enumeration value using a Data_Error
exception to catch invalid entries.


type    Choice_Type is (Encode, Decode);
package Choice_IO   is new Ada.Text_IO.Enumeration_IO (Enum =>
Choice_Type);

loop
   Ada.Text_IO.Put_Line ("Would you like to encode or decode a
message");
   begin
      Choice_IO.Get (Choice);
      exit; -- the data validation loop
   exception
      when Ada.IO_Exceptions.Data_Error =>
         Ada.Text_IO.Put_Line ("Invalid entry. Please enter Encode or
Decode.");
         Ada.Text_IO.Skip_Line;  -- Skip over the offending data
   end;
end loop;


This code is my usual pattern for data validation and works for
whatever invalid input I enter.

However, when my students leave out the call to Skip_Line in the
Data_Error exception handler the behavior depends on the actual value
entered.  It still works as desired when the input is alphabetic
characters such as “abc”.  But when the input consists of digits such
as “123” the loop becomes infinite, repeating the prompt and error
message.  It appears to me that the call to Choice_IO.Get consumes the
alphabetic input but does not consume the digit input leaving it
available for the next call to Get.  I could not find anything in the
language reference manual about how Enumeration_IO consumes input
characters when Data_Error is raised.  Can anyone give me an
explanation?  Is it implementation defined?  I am running GNAT GPL
2011.



             reply	other threads:[~2012-01-12 22:42 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-12 22:41 John McCormick [this message]
2012-01-12 23:02 ` Data_Error and Enumeration_IO Jeffrey Carter
2012-01-12 23:28 ` Georg Bauhaus
2012-01-13  0:10 ` Randy Brukardt
2012-01-13  8:33   ` Dmitry A. Kazakov
2012-01-13 21:30   ` John McCormick
2012-01-13 22:00     ` Jeffrey Carter
2012-01-14  0:09       ` Randy Brukardt
2012-01-14  9:51         ` Dmitry A. Kazakov
2012-01-14 12:25         ` Niklas Holsti
replies disabled

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