comp.lang.ada
 help / color / mirror / Atom feed
* Trouble with loop in Ada
@ 2013-01-04  4:32 Derek Wyss
  2013-01-04 10:59 ` Brian Drummond
  2013-01-04 12:44 ` Paul Colin Gloster
  0 siblings, 2 replies; 6+ messages in thread
From: Derek Wyss @ 2013-01-04  4:32 UTC (permalink / raw)


Hello,

I'm new to Ada and I'm trying to write a toy unix command shell just to 
explore the programming language.  So far I have a very small program, 
but I am confused about its behavior.

If I comment line number 11, with the Put procedure, the program loops 
through as I would expect.  However, with that line in the program, I 
have to manually press enter twice before I enter the loop.  Similarly, I 
have to hit enter one more time after the character I enter is displayed 
in order for the program to loop again.

Can anyone offer an explanation as to why I need to press enter?  I 
suspect it has to do with my use of End_Of_File and the way it "looks 
ahead" but I'm not really sure.

I'm running Linux Mint Debian Edition with the 3.2.0-4-686-pae kernel.  
I'm also using gnat, the Gnu Ada Compiler, version 4.6.  When I compile, 
I type at the command prompt:

gnatmake -gnato -fstack-check -gnatE toy_shell.adb

1  with Ada.Text_IO;
2  use Ada.Text_IO;
3
4  procedure toy_shell is
5
6    input_char: Character;
7
8  begin
9
10   while not (End_Of_File) loop
11     Put("Enter a character> ");
12     Get(input_char);
13     Put("you entered: " & input_char);
14     New_line;
15   end loop;
16
17 end toy_shell;

I hope that is enough info.  Any help would be greatly appreciated

Thanks,

Derek



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

* Re: Trouble with loop in Ada
  2013-01-04  4:32 Trouble with loop in Ada Derek Wyss
@ 2013-01-04 10:59 ` Brian Drummond
  2013-01-04 11:11   ` Brian Drummond
  2013-01-04 12:44 ` Paul Colin Gloster
  1 sibling, 1 reply; 6+ messages in thread
From: Brian Drummond @ 2013-01-04 10:59 UTC (permalink / raw)


On Fri, 04 Jan 2013 04:32:28 +0000, Derek Wyss wrote:

> Hello,
> 
> I'm new to Ada and I'm trying to write a toy unix command shell just to
> explore the programming language.  So far I have a very small program,
> but I am confused about its behavior.
 
> Can anyone offer an explanation as to why I need to press enter?  I
> suspect it has to do with my use of End_Of_File and the way it "looks
> ahead" but I'm not really sure.

The confusing thing to me is not that you have to press enter, but that 
you have to press it twice the first time round.

I started it and pressed ijklmn THEN return...
ijklmn
Enter a character> you entered: i
Enter a character> you entered: j
Enter a character> you entered: k
...
which explains it. 
(1) First, End_Of_File must block until there is input. (It has to wait 
to see if you are going to type Ctrl-C) And there isn't input until there 
is at least one character followed by a return.
(2) Then Get returns one character at a time from the line buffer, until 
it runs out, when it blocks again until there is more input.

So the behaviour comes from the library functions behind Get and 
End_Of_File rather than the loop. The question is whether you can get 
what you want from these, or need to find alternatives.

Now the good news : loops are more flexible in Ada; there are other ways 
of using them. So your immediate problem can be solved by:

  loop 
    Put("Enter a character> ");
    Get(input_char);
    Put("you entered: " & input_char);
    New_line;
    exit when End_Of_File;
  end loop;

Oh and very nicely done on the toy example - that's the way to get help!

- Brian



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

* Re: Trouble with loop in Ada
  2013-01-04 10:59 ` Brian Drummond
@ 2013-01-04 11:11   ` Brian Drummond
  0 siblings, 0 replies; 6+ messages in thread
From: Brian Drummond @ 2013-01-04 11:11 UTC (permalink / raw)


On Fri, 04 Jan 2013 10:59:24 +0000, Brian Drummond wrote:

> On Fri, 04 Jan 2013 04:32:28 +0000, Derek Wyss wrote:
> 
>> Hello,
>> 
>> I'm new to Ada and I'm trying to write a toy unix command shell just to
>> explore the programming language.  
> 
> Now the good news : loops are more flexible in Ada; there are other ways
> of using them. So your immediate problem can be solved by:

Better...

  loop 
    Put("Enter a character> ");
    exit when End_Of_File;
    Get(input_char);
    Put("you entered: " & input_char);
    New_line;
  end loop;

With the exit at the bottom of the loop, its End_Of_File test required a 
second line of input (even just a <CR>). This version performs the test 
on the same line seen by the Get. 

- Brian



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

* Re: Trouble with loop in Ada
  2013-01-04  4:32 Trouble with loop in Ada Derek Wyss
  2013-01-04 10:59 ` Brian Drummond
@ 2013-01-04 12:44 ` Paul Colin Gloster
  2013-01-04 21:27   ` Derek Wyss
  1 sibling, 1 reply; 6+ messages in thread
From: Paul Colin Gloster @ 2013-01-04 12:44 UTC (permalink / raw)


On 2013-01-04, Derek Wyss <udot7@yahoo.com> sent:
|--------------------------------------------------------------------------|
|"Hello,                                                                   |
|                                                                          |
|I'm new to Ada"                                                           |
|--------------------------------------------------------------------------|

Welcome.

|--------------------------------------------------------------------------|
|" and I'm trying to write a toy unix command shell just to                |
|explore the programming language.  So far I have a very small program,    |
|but I am confused about its behavior.                                     |
|                                                                          |
|If I comment line number 11, with the Put procedure, the program loops    |
|through as I would expect.  However, with that line in the program, I     |
|have to manually press enter twice before I enter the loop.  Similarly, I |
|have to hit enter one more time after the character I enter is displayed  |
|in order for the program to loop again.                                   |
|                                                                          |
|Can anyone offer an explanation as to why I need to press enter?"         |
|--------------------------------------------------------------------------|

Whether or not that line was commented out did not affect how many
times I pressed enter.

|--------------------------------------------------------------------------|
|"  I                                                                      |
|suspect it has to do with my use of End_Of_File and the way it "looks     |
|ahead" but I'm not really sure.                                           |
|                                                                          |
|I'm running Linux Mint Debian Edition with the 3.2.0-4-686-pae kernel.    |
|I'm also using gnat, the Gnu Ada Compiler, version 4.6.  When I compile,  |
|I type at the command prompt:                                             |
|                                                                          |
|gnatmake -gnato -fstack-check -gnatE toy_shell.adb                        |
|                                                                          |
|1  with Ada.Text_IO;                                                      |
|2  use Ada.Text_IO;                                                       |
|3                                                                         |
|4  procedure toy_shell is                                                 |
|5                                                                         |
|6    input_char: Character;                                               |
|7                                                                         |
|8  begin                                                                  |
|9                                                                         |
|10   while not (End_Of_File) loop                                         |
|11     Put("Enter a character> ");                                        |
|12     Get(input_char);                                                   |
|13     Put("you entered: " & input_char);                                 |
|14     New_line;                                                          |
|15   end loop;                                                            |
|16                                                                        |
|17 end toy_shell;                                                         |
|                                                                          |
|I hope that is enough info.  Any help would be greatly appreciated        |
|                                                                          |
|Thanks,                                                                   |
|                                                                          |
|Derek"                                                                    |
|--------------------------------------------------------------------------|


Maybe what you want is . . .

  with Ada.Text_IO;
  use Ada.Text_IO;

  procedure Adapted_Toy_Shell is

     input_char: Character;

  begin

   loop
     Put_Line("Enter a character> ");
     exit when End_Of_File(Standard_Input);
     Get(input_char);
     Put("you entered: " & input_char);
     New_line;
   end loop;

 end Adapted_Toy_Shell;

Maybe instead what you want is:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Adapted_Toy_Shell is

   input_char: Character;

begin
   Loop
      Put_Line("Enter a character> ");
      begin
         Get_Immediate(input_char);
      exception
         when End_Error =>
            exit;
      end;
      Put("you entered: " & input_char);
      New_line;
   end loop;
end Adapted_Toy_Shell;



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

* Re: Trouble with loop in Ada
  2013-01-04 12:44 ` Paul Colin Gloster
@ 2013-01-04 21:27   ` Derek Wyss
  2013-01-05  5:33     ` Randy Brukardt
  0 siblings, 1 reply; 6+ messages in thread
From: Derek Wyss @ 2013-01-04 21:27 UTC (permalink / raw)


Brian and Paul,

Thank you both so much for replying.  

Brian, thank you especially for the explanation of how End_Of_File blocks 
until there is input - that was my major hangup.  That was awesome!!

Paul, thanks for your helpful hints as well.  My next steps for improving 
my toy will be to explore 

End_Of_File vs. End_Of_File(Standard_Input) --I like the 2nd one, seems 
more clear

Get vs. Get_Immediate --That should be easy but for me it's worth some 
consideration

Then I'll look at using exceptions in my code like you did.

Again, thanks guys!  Your code examples were great and very helpful.

Derek




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

* Re: Trouble with loop in Ada
  2013-01-04 21:27   ` Derek Wyss
@ 2013-01-05  5:33     ` Randy Brukardt
  0 siblings, 0 replies; 6+ messages in thread
From: Randy Brukardt @ 2013-01-05  5:33 UTC (permalink / raw)


"Derek Wyss" <udot7@yahoo.com> wrote in message 
news:kc7hgn$gse$1@speranza.aioe.org...
...
> Paul, thanks for your helpful hints as well.  My next steps for improving
> my toy will be to explore
>
> End_Of_File vs. End_Of_File(Standard_Input) --I like the 2nd one, seems
> more clear
>
> Get vs. Get_Immediate --That should be easy but for me it's worth some
> consideration
>
> Then I'll look at using exceptions in my code like you did.

I'd suggest that you go as quickly as possible to using exceptions. It's 
almost never fool-proof to pre-test for I/O, because something else running 
on the computer could change the I/O stream between the test and the read 
(this is known as a "race condition"). By handling the exceptions, you avoid 
any problems like that, and while it doesn't really make any difference in a 
toy example, it's a good practice to get into right away. (Additionally, 
End_of_File tends to bog down performance for Text_IO; again, that doesn't 
matter in a toy program, but it's another reason to avoid it.)

This is even more true when Opening/Creating files. Just do it and handle 
the exception -- *never* try to pretest for existence - the result means 
nothing as the file could be created or deleted after the test and before 
you actually do the Open. And then you *still* have to handle the exceptions 
(unless the program is a throw-away toy, and even then, good habits are 
recommended), so why bother with the extra test?

                                              Randy.





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

end of thread, other threads:[~2013-01-05  5:33 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-04  4:32 Trouble with loop in Ada Derek Wyss
2013-01-04 10:59 ` Brian Drummond
2013-01-04 11:11   ` Brian Drummond
2013-01-04 12:44 ` Paul Colin Gloster
2013-01-04 21:27   ` Derek Wyss
2013-01-05  5:33     ` Randy Brukardt

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