comp.lang.ada
 help / color / mirror / Atom feed
* simple help with Text_IO
@ 1993-02-03  1:47 agate!spool.mu.edu!sdd.hp.com!elroy.jpl.nasa.gov!usc.edu!cs.utexas.edu!to
  0 siblings, 0 replies; 7+ messages in thread
From: agate!spool.mu.edu!sdd.hp.com!elroy.jpl.nasa.gov!usc.edu!cs.utexas.edu!to @ 1993-02-03  1:47 UTC (permalink / raw)


Could somebody tell me (email please) why this programme,
which compiles properly, seemingly stalls at the
line indicated? i.e. I type in a line followed by <cr>,
but nothing happens. What should happen is that this:

*:: <the line that was typed>

should be printed to the console, but isn't. And nothing
happens. The input prompt:

*>>

is printed, then I can type something, but after <cr>
nothing happens. :(


With Text_IO; Use Text_IO;

Procedure main is 

instr, tokstr : String(1..255);

Begin
Loop
    New_Line;
    Put("*>> ");
    Get(instr);                 -- this is where it stalls and won't go further
    New_Line;
    Put("*:: ");
    Put(instr);
    Exit When instr="quit";
End Loop;
End; --main

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

* Re: simple help with Text_IO
@ 1993-02-03 14:14 agate!spool.mu.edu!howland.reston.ans.net!bogus.sura.net!darwin.sura.net!
  0 siblings, 0 replies; 7+ messages in thread
From: agate!spool.mu.edu!howland.reston.ans.net!bogus.sura.net!darwin.sura.net! @ 1993-02-03 14:14 UTC (permalink / raw)


In article <C1unnH.Lu9@undergrad.math.waterloo.edu> ccalbrec@laplace.uwaterloo.
ca (Cory C. Albrecht) writes:

[stuff deleted]

>With Text_IO; Use Text_IO;
>
>Procedure main is 
>
>instr, tokstr : String(1..255);
                 ^^^^^^^^^^^^^^---------- a 255-character string
>
>Begin
>Loop
>    New_Line;
>    Put("*>> ");
>    Get(instr);                 -- this is where it stalls and won't go furthe
r
     ^^^^^^^^^^^------------------------- which you are trying to read here
>    New_Line;
>    Put("*:: ");
>    Put(instr);
>    Exit When instr="quit";
>End Loop;
>End; --main
>
>
>

Sigh...Ada strings are nothing magic, just arrays of characters. 

Text_IO.Get(...string...) will attempt to read _exactly_ the number of
characters specified (and it skips over CRs!). Your Get will not be 
satisfied until it gets exactly 255 characters. There's nothing "hung"
about your program; it's simply waiting for the rest of the input.

This is an extremely common misunderstanding about strings and Text_IO;
my students bump into it all the time. To solve your problem, use
Get_Line instead. Look it up in your text, or ask your teacher, or
read the LRM section describing how the various Get's work.

(Blatant plug: my freshman-level text explains it pretty well; so
do most newer books.)

Good luck -

Mike Feldman
------------------------------------------------------------------------
Michael B. Feldman
co-chair, SIGAda Education Committee

Professor, Dept. of Electrical Engineering and Computer Science
School of Engineering and Applied Science
The George Washington University
Washington, DC 20052 USA
(202) 994-5253 (voice)
(202) 994-5296 (fax)
mfeldman@seas.gwu.edu (Internet)

"Americans want the fruits of patience -- and they want them now."
------------------------------------------------------------------------

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

* Re: simple help with Text_IO
@ 1993-02-03 14:20 enterpoop.mit.edu!spool.mu.edu!howland.reston.ans.net!bogus.sura.net!jhun
  0 siblings, 0 replies; 7+ messages in thread
From: enterpoop.mit.edu!spool.mu.edu!howland.reston.ans.net!bogus.sura.net!jhun @ 1993-02-03 14:20 UTC (permalink / raw)


In <C1unnH.Lu9@undergrad.math.waterloo.edu> ccalbrec@laplace.uwaterloo.ca (Cory
 C. Albrecht) writes:

>Could somebody tell me (email please) why this programme,
>which compiles properly, seemingly stalls at the
>line indicated? i.e. I type in a line followed by <cr>,
>but nothing happens. What should happen is that this:

>*:: <the line that was typed>

>should be printed to the console, but isn't. And nothing
>happens. The input prompt:

>*>>

>is printed, then I can type something, but after <cr>
>nothing happens. :(


>With Text_IO; Use Text_IO;

>Procedure main is 

>instr, tokstr : String(1..255);

>Begin
>Loop
>    New_Line;
>    Put("*>> ");
>    Get(instr);                 -- this is where it stalls and won't go furthe
r
>    New_Line;
>    Put("*:: ");
>    Put(instr);
>    Exit When instr="quit";
>End Loop;
>End; --main

It doesn't stall, you just haven't finished entering the string yet!  Try
keep entering characters until you have entered 255 characters.

That is what get does -- What you want is Get_Line 

last : natural;

begin
loop
  new_line;
  put("*>> ");
  get_line(instr, last);
  new_line;
  Put("*:: ");
  Put(instr(1..last);

  etc.


--Thor
collard@capsrv.jhuapl.edu
dlc@ddsdx2.jhuapl.edu

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

* Re: simple help with Text_IO
@ 1993-02-03 15:17 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!howland.reston.ans.net!spool
  0 siblings, 0 replies; 7+ messages in thread
From: cis.ohio-state.edu!zaphod.mps.ohio-state.edu!howland.reston.ans.net!spool @ 1993-02-03 15:17 UTC (permalink / raw)


I think you want to change your get to a get_line. Get is trying to get
your entire input string of 255 characters, regardless of EOL. Get_line
will stop and return to you when you enter CR. (This is from some old
memory, and I am no expert, so check the ARM).
Bill Murray   whmurray@cibecue.az05.bull.com

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

* Re: simple help with Text_IO
@ 1993-02-03 16:41 Kenneth Anderson
  0 siblings, 0 replies; 7+ messages in thread
From: Kenneth Anderson @ 1993-02-03 16:41 UTC (permalink / raw)


In comp.lang.ada you write:

>Could somebody tell me (email please) why this programme,
>which compiles properly, seemingly stalls at the
>line indicated? i.e. I type in a line followed by <cr>,
>but nothing happens. What should happen is that this:

>*:: <the line that was typed>

>should be printed to the console, but isn't. And nothing
>happens. The input prompt:

>*>>

>is printed, then I can type something, but after <cr>
>nothing happens. :(


>With Text_IO; Use Text_IO;

>Procedure main is

>instr, tokstr : String(1..255);

>Begin
>Loop
>    New_Line;
>    Put("*>> ");
>    Get(instr);                 -- this is where it stalls and won't go furthe
r

It stalls here, because Get is trying to fill the string that you gave
it.  I.E. try typing a string that is 255 characters long!  I belive the
way to get around this, is to use the function Get_Line.

>    New_Line;
>    Put("*:: ");
>    Put(instr);
>    Exit When instr="quit";
>End Loop;
>End; --main


Ken

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

* Re: simple help with Text_IO
@ 1993-02-03 18:21 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!darwin.sura.net!haven.umd.ed
  0 siblings, 0 replies; 7+ messages in thread
From: cis.ohio-state.edu!zaphod.mps.ohio-state.edu!darwin.sura.net!haven.umd.ed @ 1993-02-03 18:21 UTC (permalink / raw)


In article <1993Feb3.141453.28853@seas.gwu.edu> mfeldman@seas.gwu.edu
(Michael Feldman) writes:

[stuff about the semantics for Text_Io.Get for strings]
>
>This is an extremely common misunderstanding about strings and Text_IO;
>my students bump into it all the time. To solve your problem, use
>Get_Line instead. Look it up in your text, or ask your teacher, or
>read the LRM section describing how the various Get's work.
>
>(Blatant plug: my freshman-level text explains it pretty well; so
>do most newer books.)
>

	Using Get_Line solves this problem, but creates other potential
ones. The semantics of Get_Line are not the same as for Get. Basically,
Get for strings does multiple character Gets, one for each character in
the declared string. The LRM, when describing Get for characters,
states (14.3.6):

"After skipping any line terminators and any page terminators, reads the
next character..."

	Get_Line, however, reads as follows:

"Replaces successive characters of the specified string by successive
characters read from the specified input file. Reading stops if the end
of line is met..."

	Where this wreaks havoc for my introductory students is when
they do a Get followed by a Get_Line. The line terminator hangs around
after the Get, then the Get_Line interprets it as a 0-length (null)
string. The same thing happens when, instead of a Get, a Get_Line is
done and EXACTLY the number of characters in the declared string is
entered.

	The solution is to make liberal use of Skip_Line. However, a
Skip_Line done where it ISN'T needed forces an extraneous carraige
return to be typed.

	The net effect of this is that novice programmers end up
writing, or at least contemplating briefly, lots of convoluted logic to
place Skip_Lines exactly where they are needed. My usual recommendation
is to always read more than you need, i.e.

	Input_Line : String (1..256);
	Actual_String : String (1..10); -- for example
...
	Input_Line := (others => ' ');
	Text_Io.Get_Line (Input_String, N); -- N natural, of course
	Actual_String := Input_Line (1..10);

Instead of blank filling, logic could be inserted based on the length of
the string entered. I've found that the above works best for those new
to the language (it shows array slices, use of 'others', etc.).

	Hope this helps the original poster.

>Mike Feldman

-- 
Mike Berman
University of Maryland, Baltimore County	Fastrak Training, Inc.
berman@umbc4.umbc.edu				(301)924-0050

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

* Re: simple help with Text_IO
@ 1993-02-04 18:39 agate!spool.mu.edu!uwm.edu!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu
  0 siblings, 0 replies; 7+ messages in thread
From: agate!spool.mu.edu!uwm.edu!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu @ 1993-02-04 18:39 UTC (permalink / raw)


In article <C1unnH.Lu9@undergrad.math.waterloo.edu> ccalbrec@laplace.uwaterloo.
ca (Cory C. Albrecht) writes:
>Could somebody tell me (email please) why this programme,
>which compiles properly, seemingly stalls at the
>line indicated? i.e. I type in a line followed by <cr>,
>but nothing happens. What should happen is that this:
>
>*:: <the line that was typed>
>
>should be printed to the console, but isn't. And nothing
>happens. The input prompt:
>
>*>>
>
>is printed, then I can type something, but after <cr>
>nothing happens. :(
>
>
>With Text_IO; Use Text_IO;
>
>Procedure main is 
>
>instr, tokstr : String(1..255);
>
>Begin
>Loop
>    New_Line;
>    Put("*>> ");
>    Get(instr);                 -- this is where it stalls and won't go furthe
r
>    New_Line;
>    Put("*:: ");
>    Put(instr);
>    Exit When instr="quit";
>End Loop;
>End; --main


Hi Cory,

I'm just a newcomer to ADA, so the solution I give you is not 
prefect.  But it seems to work.

Here's the program:


With Text_IO; Use Text_IO;

procedure MAIN is
  package MY_INT_IO is new INTEGER_IO(NATURAL); use MY_INT_IO;
   -- I did this so I can print the length of the string entered

instr : String(1..255);    -- The string entered goes in here
instr_len : NATURAL;       -- The length of that string will be in here.

Begin
Loop
    instr := (others => CHARACTER'FIRST);  -- Fills string full of NUL chars.
    New_Line;
    Put("*>> ");                 -- The prompt
    Get_Line(instr,instr_len);   -- instr -> String to be returned
                                 -- instr_len -> Length of string returned
    New_Line;
    Put("*:: ");                 -- Prints out what you typed
    Put_Line(instr);
    Put(" Length of input string = ");  -- Prints out length of what you typed
    Put(instr_len);
    New_Line;
    exit when instr_len = 0;     -- If you just press <CR> and nothing else
End Loop;                        -- the loop will exit & program will end
End; --main


If you remove the line "instr := (others => CHARACTER'FIRST);" the string
variable does not reinitialize it's self to NUL after each loop, or in the
Put_Line procedure.  So if the string you typed previously is longer than
the current string, you'll see some overlapping.

The line which allows you to actually exit the program:
"exit when instr_len = 0;" I changed to check the length of the 
string instead of for the word "quit".  I did this because I'm not sure
how to compare string values when they are of different lengths.
IE. the constant string "quit" is of length 4, but the variable which
contains the word 'quit', instr, is actually of length 255.....
I believe the var. instr contains the letters 'q' 'u' 'i' 't' PLUS
251 NUL characters.

As I said, I just started learning Ada, so I'm not sure how to check
for this string, any idea's anybody?


Well I hope that helps you.  I know I learned a lot from working on
the problem.




       Norm


-
Norman Russell                         |  Voice: (604)278-3411 ext. 2815
MacDonald Dettwiler and Associates     |  Fax:   (604)278-2117
13800 Commerce Parkway                 |
Richmond, B.C.   CANADA                |  Internet E-Mail: russell@mda.ca
V6V 2J3                                |

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

end of thread, other threads:[~1993-02-04 18:39 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1993-02-03 14:14 simple help with Text_IO agate!spool.mu.edu!howland.reston.ans.net!bogus.sura.net!darwin.sura.net!
  -- strict thread matches above, loose matches on Subject: below --
1993-02-04 18:39 agate!spool.mu.edu!uwm.edu!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu
1993-02-03 18:21 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!darwin.sura.net!haven.umd.ed
1993-02-03 16:41 Kenneth Anderson
1993-02-03 15:17 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!howland.reston.ans.net!spool
1993-02-03 14:20 enterpoop.mit.edu!spool.mu.edu!howland.reston.ans.net!bogus.sura.net!jhun
1993-02-03  1:47 agate!spool.mu.edu!sdd.hp.com!elroy.jpl.nasa.gov!usc.edu!cs.utexas.edu!to

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