comp.lang.ada
 help / color / mirror / Atom feed
* After typing input of Integer it SKIPS getting the inputs of String.
@ 2015-07-11 15:14 Trish Cayetano
  2015-07-11 15:36 ` Niklas Holsti
  2015-07-11 18:05 ` Jeffrey R. Carter
  0 siblings, 2 replies; 3+ messages in thread
From: Trish Cayetano @ 2015-07-11 15:14 UTC (permalink / raw)


Code:
with ada.Text_IO; use ada.Text_IO;
with ada.Integer_Text_IO; use ada.Integer_Text_IO;
procedure Main is

    
   inputText: String (1..10);
   inputNmbr : Integer;
   StringNatural: Integer;  
begin
  
   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));

end Main;

OUTPUT:

Enter Integer
2
Enter String
===================
Input for Integer: 2
Input for String: 


[2015-07-11 23:01:00] process terminated successfully, elapsed time: 00.86s

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: After typing input of Integer it SKIPS getting the inputs of String.
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Niklas Holsti @ 2015-07-11 15:36 UTC (permalink / raw)


Flip answer to "question" in Subject: Yes, it does. So what?

(That is, you could have been clearer about what you see as the problem, 
and what your question is.)

Better answer below:

On 15-07-11 18:14 , Trish Cayetano wrote:
> Code:
> with ada.Text_IO; use ada.Text_IO;
> with ada.Integer_Text_IO; use ada.Integer_Text_IO;
> procedure Main is
>
>
>     inputText: String (1..10);
>     inputNmbr : Integer;
>     StringNatural: Integer;
> begin
>
>     Put_Line("Enter Integer");
>     Get(inputNmbr,1);

The Get operation for integers leaves the "input point" after the last 
of the characters that form the integer. In this case, that point is 
before the "end of line" marker for that input line. If you then 
immediately call Get_Line, it will read characters up to the end-of-line 
marker, so it will return an empty string.

In your program, you evidently expect the Get_Line to start reading from 
the _next_ line after the line that contains the integer. So insert a 
Skip_Line between the Get(inputNmbr) and the Get_Line; that will pass 
the input point over the end-of-line so that the Get_Line starts reading 
from the start of the next line.

>     Put_Line("Enter String");
>     Get_Line(inputText,StringNatural);

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: After typing input of Integer it SKIPS getting the inputs of String.
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Jeffrey R. Carter @ 2015-07-11 18:05 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-07-11 18:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox