comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: After typing input of Integer it SKIPS getting the inputs of String.
Date: Sat, 11 Jul 2015 11:05:29 -0700
Date: 2015-07-11T11:05:29-07:00	[thread overview]
Message-ID: <mnrlqj$b0u$1@dont-email.me> (raw)
In-Reply-To: <4ab70bc8-b25f-4dc4-88d9-d7021c2bed30@googlegroups.com>

On 07/11/2015 08:14 AM, Trish Cayetano wrote:
>    Put_Line("Enter Integer");
>    Get(inputNmbr,1);
>    Put_Line("Enter String");
>    Get_Line(inputText,StringNatural);
>    Put_Line("===================");
>    Put("Input for Integer: ");
>    Put(inputNmbr,1);
>    Put_Line("");
>    Put_Line("Input for String: ");
>    Put_Line(inputText(1..StringNatural));
> 
> Enter Integer
> 2
> Enter String
> ===================
> Input for Integer: 2
> Input for String: 

You don't say exactly what characters you input to get this, but I'm guessing
you typed

2<Enter>

A lot of people either don't read or don't understand the description of
Integer_IO.Get, which is at ARM A.10.8 (8-10)

http://www.adaic.org/resources/add_content/standards/12rm/html/RM-A-10-8.html

If you had, you'd expect exactly what you got.

It reads

8
If the value of the parameter Width is zero, skips any leading blanks, line
terminators, or page terminators, then reads a plus sign if present or (for a
signed type only) a minus sign if present, then reads the longest possible
sequence of characters matching the syntax of a numeric literal without a point.
If a nonzero value of Width is supplied, then exactly Width characters are
input, or the characters (possibly none) up to a line terminator, whichever
comes first; any skipped leading blanks are included in the count.
9
Returns, in the parameter Item, the value of type Num that corresponds to the
sequence input.
10/3
The exception Data_Error is propagated if the sequence of characters read does
not form a legal integer literal or if the value obtained is not of the subtype Num.

Note that it says nothing about skipping a line terminator. So if you wanted the
String to be "Hello", you'd have to type

2Hello<Enter>

The behavior of Get is especially interesting if you want to handle input errors:

Valid_Integer : loop
   Ada.Text_IO.Put (Item => "Enter an integer: ");

   Handle_Error : begin
      Ada.Integer_Text_IO.Get (Item => I);

      exit Valid_Integer;
   exception -- Handle_Error
   when others =>
      Ada.Text_IO.Put_Line (Item => "Invalid input. Try again.");
   end Handle_Error;
end loop Valid_Integer;

If the user accidentally types 'q' for the integer, this is an infinite loop,
because nothing ever reads the 'q'. For this reason, many of us like to read a
line into a String using Get_Line and parse it using the Get with a "From : in
String" parameter.

Valid_Integer : loop
   Ada.Text_IO.Put (Item => "Enter an integer: ");

   Handle_Error : declare
      Line : constant String := Ada.Text_IO.Get_Line;

      Last : Natural;
   begin -- Handle_Error
      Ada.Integer_Text_IO.Get (From => Line, Item => I, Last => Last);

      exit Valid_Integer;
   exception -- Handle_Error
   when others =>
      Ada.Text_IO.Put_Line (Item => "Invalid input. Try again.");
   end Handle_Error;
end loop Valid_Integer;

-- 
Jeff Carter
"How'd you like to hide the egg and gurgitate
a few saucers of mocha java?"
Never Give a Sucker an Even Break
101

      parent reply	other threads:[~2015-07-11 18:05 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-11 15:14 After typing input of Integer it SKIPS getting the inputs of String Trish Cayetano
2015-07-11 15:36 ` Niklas Holsti
2015-07-11 18:05 ` Jeffrey R. Carter [this message]
replies disabled

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