comp.lang.ada
 help / color / mirror / Atom feed
* Why does this input get "skipped"?
@ 2016-08-20 22:41 John Smith
  2016-08-20 23:16 ` Jeffrey R. Carter
  2016-08-21  0:00 ` rieachus
  0 siblings, 2 replies; 12+ messages in thread
From: John Smith @ 2016-08-20 22:41 UTC (permalink / raw)


This is the snippet in question:
Ada.Text_IO.Put("Delete a value - > ");
String_Input := Ada.Text_IO.Unbounded_IO.Get_Line;
Ada.Text_IO.New_Line;

The purpose is to have the user enter some string and then keep going.  When I ran the above code, I found that I saw "Delete a value - > " being printed, but the program didn't even give me a chance to enter some form of text...

Why did my code keep running?


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

* Re: Why does this input get "skipped"?
  2016-08-20 22:41 Why does this input get "skipped"? John Smith
@ 2016-08-20 23:16 ` Jeffrey R. Carter
  2016-08-21  0:01   ` John Smith
  2016-08-21  0:00 ` rieachus
  1 sibling, 1 reply; 12+ messages in thread
From: Jeffrey R. Carter @ 2016-08-20 23:16 UTC (permalink / raw)


On 08/20/2016 03:41 PM, John Smith wrote:
> This is the snippet in question:
> Ada.Text_IO.Put("Delete a value - > ");
> String_Input := Ada.Text_IO.Unbounded_IO.Get_Line;
> Ada.Text_IO.New_Line;
> 
> The purpose is to have the user enter some string and then keep going.  When I ran the above code, I found that I saw "Delete a value - > " being printed, but the program didn't even give me a chance to enter some form of text...
> 
> Why did my code keep running?

It's impossible to be sure from the little information given, but the most
likely explanation is that there was already a line terminator waiting to be
consumed, and the call to Get_Line consumed it (and any characters preceding
it). This is often the case after a call to something like Ada.Integer_Text_IO.Get.

-- 
Jeff Carter
"People called Romanes, they go the house?"
Monty Python's Life of Brian
79


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

* Re: Why does this input get "skipped"?
  2016-08-20 22:41 Why does this input get "skipped"? John Smith
  2016-08-20 23:16 ` Jeffrey R. Carter
@ 2016-08-21  0:00 ` rieachus
  1 sibling, 0 replies; 12+ messages in thread
From: rieachus @ 2016-08-21  0:00 UTC (permalink / raw)


On Saturday, August 20, 2016 at 6:41:57 PM UTC-4, John Smith wrote:
> This is the snippet in question:
> Ada.Text_IO.Put("Delete a value - > ");
> String_Input := Ada.Text_IO.Unbounded_IO.Get_Line;
> Ada.Text_IO.New_Line;
> 
> The purpose is to have the user enter some string and then keep going.  When I ran the above code, I found that I saw "Delete a value - > " being printed, but the program didn't even give me a chance to enter some form of text...
> 
> Why did my code keep running?

There are two possibilities I see.  The first is that you expect that there is no input waiting when you output "Delete a value ->"  The call to Put will not erase any waiting input.  If there is a line waiting, in particular if End_Of_Line is true, you will get input.  If EOL is not true, and there is no EOL or EOP in the in the input buffer, it should wait.  But...  You next execute  New_Line.  If Current_Input and Current_Output are different files, no problem.  But there is no explicit barrier between the two calls, and any decent implementation of Text_IO will allow for task switches when waiting for input.  Looking at the New_Line is a bug, but I don't imagine there are any tests for it.  Consuming it would be wrong, but looking at the input and seeing a previous EOL is correct.

If you write your code:

Ada.Text_IO.Put_Line("Delete a value - > ");
String_Input := Ada.Text_IO.Unbounded_IO.Get_Line;
Ada.Text_IO.New_Line;

You should get what you want.  When working with a screen, I would not use Text_IO which was really designed for a terminal consisting of a keyboard and printer.  With Text_IO though, two good rules are: 1) A sequence of Puts should always end with a Put_Line or New_Line. 2) For input files, read input a line at a time into a String, then read from that String. Again, if you need to do other than line at a time I/O, you shouldn't beusing Text_IO.

(Confession: I wrote several Lisp drivers for specific display terminals on Multics.  When I needed to use display terminals with Ada, I knew enough to be able to treat them as full screen character displays.  Every time I thought I should create a clean Ada package instead of kludging Text_IO, but I never did.)

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

* Re: Why does this input get "skipped"?
  2016-08-20 23:16 ` Jeffrey R. Carter
@ 2016-08-21  0:01   ` John Smith
  2016-08-21  0:36     ` John Smith
  2016-08-21  0:50     ` Jeffrey R. Carter
  0 siblings, 2 replies; 12+ messages in thread
From: John Smith @ 2016-08-21  0:01 UTC (permalink / raw)


On Saturday, August 20, 2016 at 7:16:39 PM UTC-4, Jeffrey R. Carter wrote:
> On 08/20/2016 03:41 PM, John Smith wrote:
> > This is the snippet in question:
> > Ada.Text_IO.Put("Delete a value - > ");
> > String_Input := Ada.Text_IO.Unbounded_IO.Get_Line;
> > Ada.Text_IO.New_Line;
> > 
> > The purpose is to have the user enter some string and then keep going.  When I ran the above code, I found that I saw "Delete a value - > " being printed, but the program didn't even give me a chance to enter some form of text...
> > 
> > Why did my code keep running?
> 
> It's impossible to be sure from the little information given, but the most
> likely explanation is that there was already a line terminator waiting to be
> consumed, and the call to Get_Line consumed it (and any characters preceding
> it). This is often the case after a call to something like Ada.Integer_Text_IO.Get.
> 
> -- 
> Jeff Carter
> "People called Romanes, they go the house?"
> Monty Python's Life of Brian
> 79

Hi, I do have an Ada.Integer_Text_IO.Get above this call (some ways above it.)

How can I clear out that line terminator?  Another call to Get_Line, but not save the input?

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

* Re: Why does this input get "skipped"?
  2016-08-21  0:01   ` John Smith
@ 2016-08-21  0:36     ` John Smith
  2016-08-22  5:00       ` J-P. Rosen
  2016-08-22 22:18       ` rieachus
  2016-08-21  0:50     ` Jeffrey R. Carter
  1 sibling, 2 replies; 12+ messages in thread
From: John Smith @ 2016-08-21  0:36 UTC (permalink / raw)


On Saturday, August 20, 2016 at 8:01:16 PM UTC-4, John Smith wrote:
> On Saturday, August 20, 2016 at 7:16:39 PM UTC-4, Jeffrey R. Carter wrote:
> > On 08/20/2016 03:41 PM, John Smith wrote:
> > > This is the snippet in question:
> > > Ada.Text_IO.Put("Delete a value - > ");
> > > String_Input := Ada.Text_IO.Unbounded_IO.Get_Line;
> > > Ada.Text_IO.New_Line;
> > > 
> > > The purpose is to have the user enter some string and then keep going.  When I ran the above code, I found that I saw "Delete a value - > " being printed, but the program didn't even give me a chance to enter some form of text...
> > > 
> > > Why did my code keep running?
> > 
> > It's impossible to be sure from the little information given, but the most
> > likely explanation is that there was already a line terminator waiting to be
> > consumed, and the call to Get_Line consumed it (and any characters preceding
> > it). This is often the case after a call to something like Ada.Integer_Text_IO.Get.
> > 
> > -- 
> > Jeff Carter
> > "People called Romanes, they go the house?"
> > Monty Python's Life of Brian
> > 79
> 
> Hi, I do have an Ada.Integer_Text_IO.Get above this call (some ways above it.)
> 
> How can I clear out that line terminator?  Another call to Get_Line, but not save the input?

Adding a Get_Line right after the Integer_Text_IO.Get did get me the result that I want.  However, why does this feel like a kludge?


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

* Re: Why does this input get "skipped"?
  2016-08-21  0:01   ` John Smith
  2016-08-21  0:36     ` John Smith
@ 2016-08-21  0:50     ` Jeffrey R. Carter
  2016-08-21  1:04       ` John Smith
  1 sibling, 1 reply; 12+ messages in thread
From: Jeffrey R. Carter @ 2016-08-21  0:50 UTC (permalink / raw)


On 08/20/2016 05:01 PM, John Smith wrote:
> 
> Hi, I do have an Ada.Integer_Text_IO.Get above this call (some ways above it.)
> 
> How can I clear out that line terminator?  Another call to Get_Line, but not save the input?

The way to skip the next line terminator in an input file is with a call to
Skip_Line.

However, when using Text_IO for input from standard input, it's best to always
input an entire line at a time into a String, and then deal with the line in the
String. The fact that Get leaves a line terminator in the buffer wreaks havoc on
error handling.

Get_Speed : loop
   Ada.Text_IO.Put (Item => "Enter desired speed: ");

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

      Last : Natural;
   begin -- Speed_Line
      Speed_IO.Get (From => Line, Item => Speed, Last => Last);

      exit Get_Speed;
   exception -- Speed_Line
   when others =>
      Ada.Text_IO.Put_Line (Item => "Enter a valid speed.");
   end Speed_Line;
end loop Get_Speed;

-- 
Jeff Carter
"People called Romanes, they go the house?"
Monty Python's Life of Brian
79

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

* Re: Why does this input get "skipped"?
  2016-08-21  0:50     ` Jeffrey R. Carter
@ 2016-08-21  1:04       ` John Smith
  2016-08-21  4:07         ` Jeffrey R. Carter
  0 siblings, 1 reply; 12+ messages in thread
From: John Smith @ 2016-08-21  1:04 UTC (permalink / raw)


On Saturday, August 20, 2016 at 8:50:52 PM UTC-4, Jeffrey R. Carter wrote:
> On 08/20/2016 05:01 PM, John Smith wrote:
> > 
> > Hi, I do have an Ada.Integer_Text_IO.Get above this call (some ways above it.)
> > 
> > How can I clear out that line terminator?  Another call to Get_Line, but not save the input?
> 
> The way to skip the next line terminator in an input file is with a call to
> Skip_Line.
> 
> However, when using Text_IO for input from standard input, it's best to always
> input an entire line at a time into a String, and then deal with the line in the
> String. The fact that Get leaves a line terminator in the buffer wreaks havoc on
> error handling.
> 
> Get_Speed : loop
>    Ada.Text_IO.Put (Item => "Enter desired speed: ");
> 
>    Speed_Line : declare
>       Line : constant String := Ada.Text_IO.Get_Line;
> 
>       Last : Natural;
>    begin -- Speed_Line
>       Speed_IO.Get (From => Line, Item => Speed, Last => Last);
> 
>       exit Get_Speed;
>    exception -- Speed_Line
>    when others =>
>       Ada.Text_IO.Put_Line (Item => "Enter a valid speed.");
>    end Speed_Line;
> end loop Get_Speed;
> 
> -- 
> Jeff Carter
> "People called Romanes, they go the house?"
> Monty Python's Life of Brian
> 79

Just to be sure, Speed_IO.Get, you meant Ada.Integer_Text_IO.Get, yes?

I've made mistakes like this, but I want to be sure that I'm on the right page as well.

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

* Re: Why does this input get "skipped"?
  2016-08-21  1:04       ` John Smith
@ 2016-08-21  4:07         ` Jeffrey R. Carter
  0 siblings, 0 replies; 12+ messages in thread
From: Jeffrey R. Carter @ 2016-08-21  4:07 UTC (permalink / raw)


On 08/20/2016 06:04 PM, John Smith wrote:
> 
> Just to be sure, Speed_IO.Get, you meant Ada.Integer_Text_IO.Get, yes?
> 
> I've made mistakes like this, but I want to be sure that I'm on the right page as well.

Because it's a good idea to use types that reflect the problem, maybe something like

type Speed_Value is range 30 .. 150; -- Speeds the system can maintain, in kph

package Speed_IO is new Ada.Text_IO.Integer_IO (Num => Speed_Value);

-- 
Jeff Carter
"People called Romanes, they go the house?"
Monty Python's Life of Brian
79


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

* Re: Why does this input get "skipped"?
  2016-08-21  0:36     ` John Smith
@ 2016-08-22  5:00       ` J-P. Rosen
  2016-08-22 22:18       ` rieachus
  1 sibling, 0 replies; 12+ messages in thread
From: J-P. Rosen @ 2016-08-22  5:00 UTC (permalink / raw)


Le 21/08/2016 à 02:36, John Smith a écrit :
>> How can I clear out that line terminator?  Another call to
>> Get_Line, but not save the input?
> Adding a Get_Line right after the Integer_Text_IO.Get did get me the
> result that I want.  However, why does this feel like a kludge?

Because you are reading something when you don't want to read something...

There is much more in Text_IO than Get_Line/Put_Line.

In your case, what you want is to be sure that you start a clean new
line, i.e. that the current column of input is 1. The solution is:
   set_col (current_input, 1)

If you are already on column 1, it does nothing, otherwise it will skip
to the beginning of the first non-empty line.

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr


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

* Re: Why does this input get "skipped"?
  2016-08-21  0:36     ` John Smith
  2016-08-22  5:00       ` J-P. Rosen
@ 2016-08-22 22:18       ` rieachus
  2016-08-23  6:22         ` J-P. Rosen
  1 sibling, 1 reply; 12+ messages in thread
From: rieachus @ 2016-08-22 22:18 UTC (permalink / raw)


On Saturday, August 20, 2016 at 8:36:53 PM UTC-4, John Smith wrote: 
> Adding a Get_Line right after the Integer_Text_IO.Get did get me the result that I want.  However, why does this feel like a kludge?

Because Text_IO was developed thirty years ago to deal with systems that don't exist today?

BTW, always reading a line (Get_Line) at a time into a buffer, and then reading from the line with a fairly complex scanner is the way almost all Ada compilers written in Ada work.  Why?  It allows you to do more sophisticated error recognition/correction than just throwing away the bad input.

Get_Line, Skip_Line, and Set_Col(1) should all get you to the same place. (Except for some sequences involving End_Of_Page marks.) But Get_Line into a buffer allows you to at a minimum tell the user what he did wrong:

'Pinc' is not a recognized color.

   ...is an error message that shouldn't raise the user's blood pressure.  Of course, he or she might rage at "'Puce' is not a recognized color," but at least he or she will be reacting to your taste in colors, not potential input issues. ;-)  

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

* Re: Why does this input get "skipped"?
  2016-08-22 22:18       ` rieachus
@ 2016-08-23  6:22         ` J-P. Rosen
  2016-08-24 10:34           ` rieachus
  0 siblings, 1 reply; 12+ messages in thread
From: J-P. Rosen @ 2016-08-23  6:22 UTC (permalink / raw)


Le 23/08/2016 à 00:18, rieachus@comcast.net a écrit :
> Get_Line, Skip_Line, and Set_Col(1) should all get you to the same place. 
Not exactly. The benefit of using Set_Col(1) is that if you are already
on column 1, it does nothing, while others will go to the next line.

Therefore, Set_Col(1) can safely ignore whether there was a skip_line
preceding it or not.

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr

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

* Re: Why does this input get "skipped"?
  2016-08-23  6:22         ` J-P. Rosen
@ 2016-08-24 10:34           ` rieachus
  0 siblings, 0 replies; 12+ messages in thread
From: rieachus @ 2016-08-24 10:34 UTC (permalink / raw)


On Tuesday, August 23, 2016 at 2:23:05 AM UTC-4, J-P. Rosen wrote:
> Le 23/08/2016 à 00:18, rieachus@comcast.net a écrit :
> > Get_Line, Skip_Line, and Set_Col(1) should all get you to the same place. 
> Not exactly. The benefit of using Set_Col(1) is that if you are already
> on column 1, it does nothing, while others will go to the next line.
> 
> Therefore, Set_Col(1) can safely ignore whether there was a skip_line
> preceding it or not.

Correct.  I was only considering situations following a non-blank Put.  I just consider it to be very bad juju to write output without completing the line.  I haven't checked recently, but it used to be that compiler run-times would not immediately read a partial/non-completed line.  (The buffering is usually in the operating system.  The real issue is that you can't close then reopen Current_Input in an immediate mode.)


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

end of thread, other threads:[~2016-08-24 10:34 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-08-20 22:41 Why does this input get "skipped"? John Smith
2016-08-20 23:16 ` Jeffrey R. Carter
2016-08-21  0:01   ` John Smith
2016-08-21  0:36     ` John Smith
2016-08-22  5:00       ` J-P. Rosen
2016-08-22 22:18       ` rieachus
2016-08-23  6:22         ` J-P. Rosen
2016-08-24 10:34           ` rieachus
2016-08-21  0:50     ` Jeffrey R. Carter
2016-08-21  1:04       ` John Smith
2016-08-21  4:07         ` Jeffrey R. Carter
2016-08-21  0:00 ` rieachus

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