comp.lang.ada
 help / color / mirror / Atom feed
From: Jerry <lanceboyle@qwest.net>
Subject: How does Ada.Text_IO.Enumeration_IO work?
Date: Thu, 3 Nov 2011 01:52:35 -0700 (PDT)
Date: 2011-11-03T01:52:35-07:00	[thread overview]
Message-ID: <865d6298-5a0b-45c5-b0fc-372d6222752f@z22g2000prd.googlegroups.com> (raw)

I don't understand how the following program works, specifically, how
instances of Ada.Text_IO.Enumeration_IO read input. I understand that
it will skip leading white space, leave anything after finding a
proper element including line terminator, that apostophe ' is a valid
character, and that it can raise an exception (quoting ARM):

"The exception Data_Error is propagated if the sequence input does not
have the required syntax, or if the identifier or character literal
does not correspond to a value of the subtype Enum."

I want the program to read input until a valid element is found, then
quit.


with Ada.Text_IO; use Ada.Text_IO;
with Ada.IO_Exceptions; use Ada.IO_Exceptions;
procedure Day_Proc is
    type Day_Type is (Sunday, Monday, Tuesday);
    Day : Day_Type;
    package Day_Type_IO is new Ada.Text_IO.Enumeration_IO(Day_Type);
    Have_Good_Value : Boolean;
begin
    Put("Enter a day: ");
    loop
        Have_Good_Value := True;
        begin
            Day_Type_IO.Get(Day);
            --Skip_Line; -- Doesn't matter if present or not.
        exception
        when Ada.IO_Exceptions.Data_Error =>
            Have_Good_Value := False;
            Put_Line("Exception raised");
        end;
        Put_Line(Boolean'image(Have_Good_Value));
        exit when Have_Good_Value;
    end loop;
    Put_Line("Your day is " & Day_Type'image(Day));
end Day_Proc;


This works as I expect for inputs such as

   Monday
Monday Friday
Friday Monday
Fri7day Monday
Friday ' Monday
Monday.
 etc.

but anytime the input contains a non-apostrophe punctuation mark
before a valid element, or an otherwise inproperly syntaxed element,
it loops endlessly, outputting Exception raised and FALSE for each
passage through the loop. For instance, these lines cause infinite
looping:

Friday. Monday
Friday.Monday
Friday ? Monday
Fri?day Monday
Friday 7Thursday Monday
This is (or is not) a comment
 etc.

It is correctly raising the exception upon encountering the bad input,
but why does it keep looping and not proceed past the bad input to
find the following correct element?

Jerry



             reply	other threads:[~2011-11-03  8:53 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-03  8:52 Jerry [this message]
2011-11-03  9:27 ` How does Ada.Text_IO.Enumeration_IO work? AdaMagica
2011-11-03 10:18   ` Jerry
2011-11-03 14:52 ` Adam Beneschan
2011-11-04 22:46   ` Jerry
2011-11-04 23:31     ` Adam Beneschan
2011-11-05  9:32       ` Dmitry A. Kazakov
2011-11-06 22:42       ` Jerry
replies disabled

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