comp.lang.ada
 help / color / mirror / Atom feed
* Help: Get/Get_line
@ 1998-05-16  0:00 John Slavich
  1998-05-16  0:00 ` Tom Moran
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: John Slavich @ 1998-05-16  0:00 UTC (permalink / raw)



Hello there,

	As you might have guessed by the object of this email, I'm in
relatively new at this ADA game.  I'm at an impass and none of my
references can give me the answer to my problem.  The following works
fine:
...
number  : integer;
number2 : integer;
name    : string (1..50);
...
begin
	get_line(name, number2);
	...
	get(number);
... 

But when if I invert them, GNAT is not happy and it doesn't work:

...
begin
	get(number);
	...
	get_line(name, number2);
... 

What am I missing?
Thanks in advance for your help.  JOhn




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

* Re: Help: Get/Get_line
  1998-05-16  0:00 Help: Get/Get_line John Slavich
@ 1998-05-16  0:00 ` Tom Moran
  1998-05-17  0:00   ` John Slavich
  1998-05-16  0:00 ` Matthew Heaney
  1998-05-20  0:00 ` christoph grein
  2 siblings, 1 reply; 11+ messages in thread
From: Tom Moran @ 1998-05-16  0:00 UTC (permalink / raw)



What does
> GNAT is not happy and it doesn't work
mean?





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

* Re: Help: Get/Get_line
  1998-05-16  0:00 Help: Get/Get_line John Slavich
  1998-05-16  0:00 ` Tom Moran
@ 1998-05-16  0:00 ` Matthew Heaney
  1998-05-20  0:00 ` christoph grein
  2 siblings, 0 replies; 11+ messages in thread
From: Matthew Heaney @ 1998-05-16  0:00 UTC (permalink / raw)



In article <355E0DE6.6867@info.polymtl.ca>, jslavich@info.polymtl.ca wrote:

(start of quote)
number  : integer;
number2 : integer;
name    : string (1..50);
...
begin
        get_line(name, number2);
        ...
        get(number);
... 
(end of quote)

The value returned as the second value of Get_Line is called Last, and
refers to the last index in that contains a valid value read in.  It is
prefered that you name that object last, as in

declare
   Name : String (1 .. 50);
   Last : Natural;
begin
   Get_Line (Name, Last);
...
end;

(end of quote)
But when if I invert them, GNAT is not happy and it doesn't work:

...
begin
        get(number);
        ...
        get_line(name, number2);
... 

What am I missing?
(end of quote)

What do you mean by "GNAT is not happy." Do you mean that the program
wouldn't compile, or that it does compile, but runs with an exception, or
runs but delivers incorrect results?

It is difficult to provide help when you use non-technical descriptions as
"not happy."

And what is Get?  Is it from Ada.Integer_Text_IO?  Or some instantiation of
Ada.Text_IO.Integer_IO?  Or something else?

If you're getting an exception during Get, it may mean that you're trying
to enter a number that doesn't have the format required for an integer
literal.




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

* Re: Help: Get/Get_line
  1998-05-17  0:00   ` John Slavich
@ 1998-05-17  0:00     ` Matthew Heaney
  1998-05-17  0:00       ` John Slavich
  0 siblings, 1 reply; 11+ messages in thread
From: Matthew Heaney @ 1998-05-17  0:00 UTC (permalink / raw)



In article <355E79FE.448@info.polymtl.ca>, jslavich@info.polymtl.ca wrote:

(start of quote)
My problem is when I need
to invert the "get" and "get_line" like so:
...
begin
        get(number);
        ...
        get_line(name, number2); ...
end xx; 

        At the first prompt I enter an integer and as soon as I hit the return
key, it skips over the "get_line" command and doesn't prompt me to enter
a string.  I get no error from the "get_line" command.
(end of quote)

Here's your problem:

When you press Return to enter the value of Number at the prompt, Get reads
in and parses only the number itself, not the carriage return.  What
happens is that when you say Get_Line, the carriage return is still there,
and it reads _that_ one, instead of the one you want.

What you need to do is just add a statement that consumes the rest in the
line, including the carriage return, and then do the Get_Line.  So just add
a Skip_Line immediately following Get.

The code belows compiles and works as expected, using GNAT 3.10p under UNIX.

Hope that helps,
Matt

--STX
with Ada.Integer_Text_IO;
with Ada.Text_IO;

procedure Test_IO is

   Name : String (1 .. 50);
   Last : Natural;

   I : Integer;

begin

   Ada.Integer_Text_IO.Get (I);

--!!! Here's the new line:
   Ada.Text_IO.Skip_Line;

   Ada.Text_IO.Get_Line (Name, Last);

   Ada.Text_IO.Put_Line ("I is" & I'Img);
   Ada.Text_IO.Put_Line ("Name is '" & Name (1 .. Last) & "'");

end;




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

* Re: Help: Get/Get_line
  1998-05-17  0:00     ` Matthew Heaney
@ 1998-05-17  0:00       ` John Slavich
  1998-05-18  0:00         ` Dale Stanbrough
  0 siblings, 1 reply; 11+ messages in thread
From: John Slavich @ 1998-05-17  0:00 UTC (permalink / raw)



Thanks a lot for your help and the explanation, it works well.

JOhn




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

* Re: Help: Get/Get_line
  1998-05-16  0:00 ` Tom Moran
@ 1998-05-17  0:00   ` John Slavich
  1998-05-17  0:00     ` Matthew Heaney
  0 siblings, 1 reply; 11+ messages in thread
From: John Slavich @ 1998-05-17  0:00 UTC (permalink / raw)



Thanks Tom and Matthew for replying to my message.
	I apologize for being so unclear the first time, I will now try to
redeem myself.
	
	In my first example:

with Ada.Text_Io, Ada.Integer_Text_Io; 
use Ada.Text_Io,  Ada.Integer_Text_Io;
...
number, number2: integer; name: string (1..50);
...
begin
        get_line(name, number2);
        ...
        get(number); ...
end xx;

	In this situation, everything works fine, i.e., I get a first prompt in
my window where I just enter a string, after pressing "return" I get a
second prompt for an integer and all is well. My problem is when I need
to invert the "get" and "get_line" like so:
...
begin
        get(number);
        ...
        get_line(name, number2); ...
end xx; 

	At the first prompt I enter an integer and as soon as I hit the return
key, it skips over the "get_line" command and doesn't prompt me to enter
a string.  I get no error from the "get_line" command.  I do get an
error when I want to use the content of the "name" variable (e.g.,
openning a file), but if I don't use the "name" variable after the
"get_line", I get no error whatsoever.  During my troubleshooting, I
checked the contents of "name" before and after the "get_line" command
and it remains unchanged (i.e. just junk). (I'm using GNAT on Win-95.)

	I hope that I was a little clearer and that my explanation makes sense.

Again thanks for your help. JOhn




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

* Re: Help: Get/Get_line
  1998-05-17  0:00       ` John Slavich
@ 1998-05-18  0:00         ` Dale Stanbrough
  0 siblings, 0 replies; 11+ messages in thread
From: Dale Stanbrough @ 1998-05-18  0:00 UTC (permalink / raw)



John Slavich writes:

"Thanks a lot for your help and the explanation, it works well.
 
 JOhn"


I'll make my program that demonstrates the various input routines
available from the text_io family on my web page soon. It allows you
to see where the file pointer currently is, undo operations etc.


Dale




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

* Re: Help: Get/Get_line
  1998-05-16  0:00 Help: Get/Get_line John Slavich
  1998-05-16  0:00 ` Tom Moran
  1998-05-16  0:00 ` Matthew Heaney
@ 1998-05-20  0:00 ` christoph grein
  1998-05-21  0:00   ` John J Cupak Jr, CCP
  2 siblings, 1 reply; 11+ messages in thread
From: christoph grein @ 1998-05-20  0:00 UTC (permalink / raw)



This problem has a very simple solution, but the cause is not very
obvious. I've already sent the solution to someone before with the
request
to forward it to cla, but he didn't do this. So please forward the
solution.

You have to read the RM about Text_IO very carefully.

  get(number);

reads as many characters as can be interpreted as a whole number and
converts
this to the result type. Any other characters stay in the input stream.

So e.g. if you input "123.45", you will get "number=123", the next
"get_line(name, last);" will return ".45..." and any characters until
EOL or
name'last is reached, whatever occurs first.

So if you write

  get(number);
  get_line(name, last);

and enter on you keyboard

1234<return>

"get(number);" reads 1234, but leaves EOL, which is then immediately
read by
"get_line(name, last);", which will return "name" empty (unchanged) and
"Last=0".

This is quite confusing sometimes even to experienced programmers.

I hope this helps to let you sort out the solution of your problem.


Christoph




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

* Re: Help: Get/Get_line
  1998-05-20  0:00 ` christoph grein
@ 1998-05-21  0:00   ` John J Cupak Jr, CCP
  1998-05-22  0:00     ` Assistance: Get/Get_line Anonymous
  1998-05-22  0:00     ` Help: Get/Get_line Dale Stanbrough
  0 siblings, 2 replies; 11+ messages in thread
From: John J Cupak Jr, CCP @ 1998-05-21  0:00 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 1080 bytes --]

Christoph Grein has pointed out the problem with using Get to read
an Integer number from the input stream, as opposed to using Get_Line
to read the entire line.

In the Ada classes I teach, I recommend that the students use
Skip_Line to ignore everything after the initial input:

   Get(Number); -- Read Integer number
   Skip_Line;   -- Discard remainder of input buffer

Alternatively, if you use Get_Line(Line, Size) to read in the
entire line, then you can use the Integer Get from a string to
extract just the integer part (See RM A.10.8(15-16)):

   Get_Line(Line, Size);
   Get(From => Line(1..Size), Item => Number, Last => Last);

Hope this helps! (And that I didn't make any misteaks!)

-- 
--------------------------------------------------------------
-                 John J. Cupak Jr, CCP                      -
- Raytheon Systems Company - Software Engineering Laboratory -
- tel: 978-858-1222   email (work): jcj@swl.msd.ray.com      -
- fax: 978-858-4336   email (home): jcupak@aol.com           -
--------------------------------------------------------------

[-- Attachment #2: Card for Cupak Jr, CCP, John J. --]
[-- Type: text/x-vcard, Size: 520 bytes --]

begin:          vcard
fn:             John J. Cupak Jr, CCP
n:              Cupak Jr, CCP;John J.
org:            Raytheon System Company / SEL Training
adr;dom:        50 Apple Hill Road;;MS T3MN35;Tewksbury;MA;01879;
email;internet: jcj@swl.msd.ray.com
title:          Senior Software Engineer / Instructor
tel;work:       978-858-1222
tel;fax:        978-858-4336
tel;home:       603-595-8240
note:           EMAIL;AOL:jcupak@aol.com
x-mozilla-cpt:  ;0
x-mozilla-html: TRUE
version:        2.1
end:            vcard


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

* Re: Help: Get/Get_line
  1998-05-21  0:00   ` John J Cupak Jr, CCP
  1998-05-22  0:00     ` Assistance: Get/Get_line Anonymous
@ 1998-05-22  0:00     ` Dale Stanbrough
  1 sibling, 0 replies; 11+ messages in thread
From: Dale Stanbrough @ 1998-05-22  0:00 UTC (permalink / raw)



Dale Stanbrough writes:

"I'll make my program that demonstrates the various input routines
 available from the text_io family on my web page soon. It allows you
 to see where the file pointer currently is, undo operations etc."


...and it is available at...


	www.cs.rmit.edu.au/~dale/io_view/io_view.tar.gz


compiles under gnat 3.10 on solaris, requires a simple ansi based
screen (not quite up to Java GUI standards... :-)


Dale




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

* Re: Assistance: Get/Get_line
  1998-05-21  0:00   ` John J Cupak Jr, CCP
@ 1998-05-22  0:00     ` Anonymous
  1998-05-22  0:00     ` Help: Get/Get_line Dale Stanbrough
  1 sibling, 0 replies; 11+ messages in thread
From: Anonymous @ 1998-05-22  0:00 UTC (permalink / raw)



<355E0DE6.6867@info.polymtl.ca> <356231EB.3F2@eurocopter.de>

On Thu, 21 May 1998 12:29:24 -0400, "John J Cupak Jr, CCP"
<jcj@swl.msd.ray.com> wrote:

[about using Text_Io to input Integers (or numbers in general)]

> ...
> Alternatively, if you use Get_Line(Line, Size) to read in the
> entire line, then you can use the Integer Get from a string to
> extract just the integer part (See RM A.10.8(15-16)):
> 
>    Get_Line(Line, Size);
>    Get(From => Line(1..Size), Item => Number, Last => Last);
> ...

This (or using 'Value) is similar to the approach I recommend. I either
use a "function Get_Line return String;" or put

if Size >= Line'Last then
   Skip_Line;
end if;

after the Get_Line. Then I'm sure the entire line is gone. If there is
an error in the input, error handling is simplified if the line is gone.
Using "Get (Number);" when there is an error in the input, the required
error handling is difficult. I've seen many programs that do

output prompt

loop
   begin
      Get (Number);

      exit;
   exception
   when others =>
      output "Invalid number; re-enter"
   end;
end loop;

that work fine when the input is correct. Type an "X" for the number and
it goes into an infinite loop. Using Get_Line and really getting the
entire line eliminates this common error.

Jeff Carter  PGP:1024/440FBE21
My real e-mail address: ( carter @ innocon . com )
"Now go away or I shall taunt you a second time."
Monty Python & the Holy Grail

Posted with Spam Hater - see
http://www.compulink.co.uk/~net-services/spam/




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

end of thread, other threads:[~1998-05-22  0:00 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-05-16  0:00 Help: Get/Get_line John Slavich
1998-05-16  0:00 ` Tom Moran
1998-05-17  0:00   ` John Slavich
1998-05-17  0:00     ` Matthew Heaney
1998-05-17  0:00       ` John Slavich
1998-05-18  0:00         ` Dale Stanbrough
1998-05-16  0:00 ` Matthew Heaney
1998-05-20  0:00 ` christoph grein
1998-05-21  0:00   ` John J Cupak Jr, CCP
1998-05-22  0:00     ` Assistance: Get/Get_line Anonymous
1998-05-22  0:00     ` Help: Get/Get_line Dale Stanbrough

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