comp.lang.ada
 help / color / mirror / Atom feed
* Problem with output using Ada.Text_IO.Put
@ 2014-07-19 21:37 junior learning ADA
  2014-07-19 22:10 ` Jerry
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: junior learning ADA @ 2014-07-19 21:37 UTC (permalink / raw)


I am starting to learn ADA and I have installed Adacore gnat 2014. 

I am using the source codes from M.B. Feldman and E.B. Koffman, Ada 95 Problem Solving and Program Design, 3rd edition.

In this respect, there appears to be a problem with Ada.Text_IO.Put and Ada.Integer_Text_IO.Get. The issue is that the text output only appears after the Ada.Integer_Text_IO.Get (Item => Nickels);

Please see extract from program:

BEGIN -- Coin_Collection

  -- prompt user for number of nickels and pennies
  Ada.Text_IO.Put (Item => "How many nickels do you have? ");
  Ada.Integer_Text_IO.Get (Item => Nickels);
  Ada.Text_IO.Put (Item => "How many pennies do you have? ");
  Ada.Integer_Text_IO.Get (Item => Pennies);
  Ada.Text_IO.New_Line;


However, when I run the program the text output appears after the questions. See below:
/Users/francois/Documents/ADA/test/fk3-w95/coin_collection
1
2
How many nickels do you have? How many pennies do you have? 
[2014-07-19 22:05:16] process terminated successfully, elapsed time: 16.28s

I guess my explanation is not very clear, but what I would have expected the program to do is:
How many nickels do you have? 
(then we enter 1 for example)
How many pennies do you have? 
(then we enter 2 for example)

Is the issue coming from using a newer version of ADA?

Francois


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

* Re: Problem with output using Ada.Text_IO.Put
  2014-07-19 21:37 Problem with output using Ada.Text_IO.Put junior learning ADA
@ 2014-07-19 22:10 ` Jerry
  2014-07-19 23:58 ` Jeffrey Carter
  2014-07-20  5:22 ` Niklas Holsti
  2 siblings, 0 replies; 9+ messages in thread
From: Jerry @ 2014-07-19 22:10 UTC (permalink / raw)


Works for me.

with Ada.Text_IO;
with Ada.Integer_Text_IO;
procedure textfromcla is
    Nickels, Pennies : Integer;
begin
    Ada.Text_IO.Put (Item => "How many nickels do you have? "); 
    Ada.Integer_Text_IO.Get (Item => Nickels); 
    Ada.Text_IO.Put (Item => "How many pennies do you have? "); 
    Ada.Integer_Text_IO.Get (Item => Pennies); 
    Ada.Text_IO.New_Line;
end textfromcla;

causes

How many nickels do you have? 3
How many pennies do you have? 5

Jerry

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

* Re: Problem with output using Ada.Text_IO.Put
  2014-07-19 21:37 Problem with output using Ada.Text_IO.Put junior learning ADA
  2014-07-19 22:10 ` Jerry
@ 2014-07-19 23:58 ` Jeffrey Carter
  2014-07-20  5:22 ` Niklas Holsti
  2 siblings, 0 replies; 9+ messages in thread
From: Jeffrey Carter @ 2014-07-19 23:58 UTC (permalink / raw)


On 07/19/2014 02:37 PM, junior learning ADA wrote:
> I am starting to learn ADA and I have installed Adacore gnat 2014.

Good. Is that the American Dental Association or the Americans with Disabilities 
Act that you're learning? Ada is a woman's name (Ada King, Countess of Lovelace, 
who wrote programs for Babbage's Analytical Engine), not an acronym.

> BEGIN -- Coin_Collection
>
>    -- prompt user for number of nickels and pennies
>    Ada.Text_IO.Put (Item => "How many nickels do you have? ");
>    Ada.Integer_Text_IO.Get (Item => Nickels);
>    Ada.Text_IO.Put (Item => "How many pennies do you have? ");
>    Ada.Integer_Text_IO.Get (Item => Pennies);
>    Ada.Text_IO.New_Line;
>
>
> However, when I run the program the text output appears after the questions. See below:
> /Users/francois/Documents/ADA/test/fk3-w95/coin_collection
> 1
> 2
> How many nickels do you have? How many pennies do you have?
> [2014-07-19 22:05:16] process terminated successfully, elapsed time: 16.28s

That's very odd. I can't say that I've seen that behavior before.

> Is the issue coming from using a newer version of ADA?

No, Ada.Text_IO should work the same.

-- 
Jeff Carter
"All citizens will be required to change their underwear
every half hour. Underwear will be worn on the outside,
so we can check."
Bananas
29

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

* Re: Problem with output using Ada.Text_IO.Put
  2014-07-19 21:37 Problem with output using Ada.Text_IO.Put junior learning ADA
  2014-07-19 22:10 ` Jerry
  2014-07-19 23:58 ` Jeffrey Carter
@ 2014-07-20  5:22 ` Niklas Holsti
  2014-07-20  6:52   ` junior learning ADA
  2 siblings, 1 reply; 9+ messages in thread
From: Niklas Holsti @ 2014-07-20  5:22 UTC (permalink / raw)


On 14-07-20 00:37 , junior learning ADA wrote:
> I am starting to learn ADA and I have installed Adacore gnat 2014. 

Welcome to the Ada community! And don't be offended if some of us insist
that it should be spelled Ada instead of ADA. This spelling thing is
something of an "in-joke" here. Now that you have been subjected to it,
you can consider yourself initiated into the community.

> I am using the source codes from M.B. Feldman and E.B. Koffman,
> Ada 95 Problem Solving and Program Design, 3rd edition.
> 
> In this respect, there appears to be a problem with Ada.Text_IO.Put
> and Ada.Integer_Text_IO.Get. The issue is that the text output
> only appears after the Ada.Integer_Text_IO.Get (Item => Nickels);

As others have answered, interactive Text_IO should not behave in that
way, but should behave as you expect it to do. (However -- and this is a
question for the other responders -- I did not yet find where in the Ada
Reference Manual this interactive behaviour is specified.)

> Please see extract from program:
> 
> BEGIN -- Coin_Collection
> 
>   -- prompt user for number of nickels and pennies
>   Ada.Text_IO.Put (Item => "How many nickels do you have? ");
>   Ada.Integer_Text_IO.Get (Item => Nickels);
>   Ada.Text_IO.Put (Item => "How many pennies do you have? ");
>   Ada.Integer_Text_IO.Get (Item => Pennies);
>   Ada.Text_IO.New_Line;
> 
> 
> However, when I run the program the text output appears after the questions. See below:
> /Users/francois/Documents/ADA/test/fk3-w95/coin_collection
> 1
> 2
> How many nickels do you have? How many pennies do you have? 
> [2014-07-19 22:05:16] process terminated successfully, elapsed time: 16.28s

It seems as if the process that executes your Ada program is treating
the Text_IO standard-output channel as a block-buffered channel, and is
not flushing the collected output when the program asks for some input
on the standard-input channel, as should happen for interactive,
non-buffered channels.

Are you running the program from a normal shell in a normal command
window, or in some special way? Which OS are you using? Are you using
some output-collecting tool such as "tee"? Please show us the command
line you use to run your program.

To understand the problem better, you could make some experiments:

- Try using Ada.Text_IO.Put_Line instead of Put (or, alternatively, add
a call of Ada.Text_IO.New_Line after each Put call). This experiment
should show us if your system treats standard-output as line-buffered
(but from the output you display, this is not the case).

- Try adding a call of Ada.Text_IO.Flush after every call to Put, at the
point where you expect the output from the Put to be visible as a prompt
for the user's input. This may counteract the output buffering that, for
some reason, is happening in your environment.


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

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

* Re: Problem with output using Ada.Text_IO.Put
  2014-07-20  5:22 ` Niklas Holsti
@ 2014-07-20  6:52   ` junior learning ADA
  2014-07-20  7:00     ` junior learning ADA
                       ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: junior learning ADA @ 2014-07-20  6:52 UTC (permalink / raw)


Niklas,

Thank you for the answer.

I am using a Mac with OSX and I have properly installed Quartz.

If I run the program in an external terminal, launched through GPS, then It works properly. Except that the terminal closes automatically, I don't see the result of my program.

On the other hand, if I run it within GPS, then I have the issue of having everything appearing as a block as you correctly pointed it out.

It may be an issue specific to GPS on Mac, and it may be a point worth reporting to Adacore.

Is there a possibility to run an application from GPS and keep the terminal open once the process has terminated. This way it should be possible to see the result of the program?

Francois

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

* Re: Problem with output using Ada.Text_IO.Put
  2014-07-20  6:52   ` junior learning ADA
@ 2014-07-20  7:00     ` junior learning ADA
  2014-07-20 14:51       ` Niklas Holsti
  2014-07-20  8:53     ` Georg Bauhaus
  2014-07-20 10:36     ` Simon Wright
  2 siblings, 1 reply; 9+ messages in thread
From: junior learning ADA @ 2014-07-20  7:00 UTC (permalink / raw)


Spot on.

Adding Ada.Text_IO.New_Line solved the issue.

Thank you very much,

Francois

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

* Re: Problem with output using Ada.Text_IO.Put
  2014-07-20  6:52   ` junior learning ADA
  2014-07-20  7:00     ` junior learning ADA
@ 2014-07-20  8:53     ` Georg Bauhaus
  2014-07-20 10:36     ` Simon Wright
  2 siblings, 0 replies; 9+ messages in thread
From: Georg Bauhaus @ 2014-07-20  8:53 UTC (permalink / raw)


On 20/07/14 08:52, junior learning ADA wrote:
> Is there a possibility to run an application from GPS and keep the terminal open once the process has terminated. This way it should be possible to see the result of the program?

An easy way of keeping a terminal open is---pardon me for
sticking to the literal---actually opening one. Then change
directories to where your IDE places your project. You can
switch from GPS to terminal windows/screens and back simply
using the usual OS keys/gestures.

This way, you'll take full control of how you start the program
or how many times, and of what is done with its input and output.


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

* Re: Problem with output using Ada.Text_IO.Put
  2014-07-20  6:52   ` junior learning ADA
  2014-07-20  7:00     ` junior learning ADA
  2014-07-20  8:53     ` Georg Bauhaus
@ 2014-07-20 10:36     ` Simon Wright
  2 siblings, 0 replies; 9+ messages in thread
From: Simon Wright @ 2014-07-20 10:36 UTC (permalink / raw)


junior learning ADA <francois.chesnay@gmail.com> writes:

> If I run the program in an external terminal, launched through GPS,
> then It works properly. Except that the terminal closes automatically,
> I don't see the result of my program.

I can't try this directly, because I'm running GPS GPL 2014 on Mac OS X
(Mavericks) without Quartz (good for AdaCore!) and so I have no xterm.

A bit of googling led me to [1]; look at the second answer (use -hold).

In GPS, Edit > Preferences > External Command > Execute command, change
"xterm -e" to "xterm -hold -e".

Good luck!

[1] http://superuser.com/questions/363614/leave-xterm-open-after-task-is-complete


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

* Re: Problem with output using Ada.Text_IO.Put
  2014-07-20  7:00     ` junior learning ADA
@ 2014-07-20 14:51       ` Niklas Holsti
  0 siblings, 0 replies; 9+ messages in thread
From: Niklas Holsti @ 2014-07-20 14:51 UTC (permalink / raw)


On 14-07-20 10:00 , junior learning ADA wrote:
> Spot on.
> 
> Adding Ada.Text_IO.New_Line solved the issue.

Good. Did you try it with Ada.Text_IO.Flush, too? If Flush also works,
you could keep the prompt and the answer on the same line, as you intended.

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


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

end of thread, other threads:[~2014-07-20 14:51 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-19 21:37 Problem with output using Ada.Text_IO.Put junior learning ADA
2014-07-19 22:10 ` Jerry
2014-07-19 23:58 ` Jeffrey Carter
2014-07-20  5:22 ` Niklas Holsti
2014-07-20  6:52   ` junior learning ADA
2014-07-20  7:00     ` junior learning ADA
2014-07-20 14:51       ` Niklas Holsti
2014-07-20  8:53     ` Georg Bauhaus
2014-07-20 10:36     ` Simon Wright

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