comp.lang.ada
 help / color / mirror / Atom feed
From: JohnVolan@macconnect.com (John Volan)
Subject: Re: Exception problem
Date: 1997/03/09
Date: 1997-03-09T00:00:00+00:00	[thread overview]
Message-ID: <JohnVolan-0903971008050001@accs-as34-dp04.snfc.grid.net> (raw)
In-Reply-To: 5eutjo$rlg@ash.ridgecrest.ca.us


> I would have written the program almost exactly the way you did.  The 
> only difference is that I would have used "Get_Line(TEXT, LENGTH);" 
> instead of Get.  That would have eliminated the need for the Skip_Line.
> 
> After obtaining the text from the user, I would have used the 'VALUE 
> attribute to convert TEXT(1 .. LENGTH) to the integer type, if possible.  
> The 'VALUE attribute will raise an exception if the text string isn't an 
> integer, so you would have no choice but to use an exception handler.

The disadvantage in using 'Value is that it is not very forgiving if the
input is not *exactly* an integer literal, i.e., if there is white space
before and/or after the integer literal within the input string. An
alternative method is to go ahead and read the input line into a string
buffer, but then use a Get procedure from an instantiation of
Ada.Text_IO.Integer_IO:

    Get (Text, Length);
    Get (From => Text (1 .. Length), -- eats any leading whitespace
         Item => Your_Integer,
         Last => Next); -- stops at the end of the integer literal,
                        -- before any trailing whitespace (or indeed,
                        -- any trailing anything
    -- optionally, if you want to do something with the rest of the input line,
    -- you can do:
    Next := Next + 1;
    -- and then do further processing on Text (Next .. Length)
> I would have written the program almost exactly the way you did.  The 
> only difference is that I would have used "Get_Line(TEXT, LENGTH);" 
> instead of Get.  That would have eliminated the need for the Skip_Line.
> 
> After obtaining the text from the user, I would have used the 'VALUE 
> attribute to convert TEXT(1 .. LENGTH) to the integer type, if possible.  
> The 'VALUE attribute will raise an exception if the text string isn't an 
> integer, so you would have no choice but to use an exception handler.

The disadvantage in using 'Value is that it is not very forgiving if the
input is not *exactly* an integer literal, i.e., if there is white space
before and/or after the integer literal within the input string. An
alternative method is to go ahead and read the input line into a string
buffer, but then use a Get procedure from an instantiation of
Ada.Text_IO.Integer_IO:

    Get (Text, Length);
    Get (From => Text (1 .. Length), -- eats any leading whitespace
         Item => Your_Integer,
         Last => Next); -- stops at the end of the integer literal,
                        -- before any trailing whitespace (or indeed,
                        -- any trailing anything
    -- optionally, if you want to do something with the rest of the input line,
    -- you can do:
    Next := Next + 1;
    -- and then do further processing on Text (Next .. Length)

----------------------------------------------------------------------
Internet.Usenet.Put_Signature 
  (Name => "John G. Volan", Home_Email => "johnvolan@macconnect.com",
   Slogan => "Ada95: The World's *FIRST* International-Standard OOPL",
   Disclaimer => "No one ever defined these opinions, so using them" 
     & "is totally erroneous...or is that a bounded error now? :-) ");
----------------------------------------------------------------------




  reply	other threads:[~1997-03-09  0:00 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-02-18  0:00 Exception problem Larry Coon
1997-02-18  0:00 ` Michael Feldman
1997-02-19  0:00   ` Larry Coon
1997-02-19  0:00 ` Keith Allan Shillington
1997-02-19  0:00 ` David C. Hoos, Sr.
1997-02-19  0:00   ` Larry Coon
1997-02-19  0:00   ` Robert Dewar
1997-02-19  0:00 ` Joerg Rodemann
1997-02-19  0:00   ` Mats Weber
1997-02-22  0:00 ` Arthur Evans Jr
1997-02-24  0:00   ` Larry Coon
1997-02-24  0:00     ` Larry Kilgallen
1997-02-24  0:00       ` Larry Coon
1997-02-25  0:00         ` Fergus Henderson
1997-02-25  0:00     ` Do-While Jones
1997-03-09  0:00       ` John Volan [this message]
1997-03-09  0:00         ` Robert Dewar
1997-03-12  0:00         ` Keith Thompson
1997-02-25  0:00   ` Robert I. Eachus
replies disabled

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