comp.lang.ada
 help / color / mirror / Atom feed
From: cis.ohio-state.edu!zaphod.mps.ohio-state.edu!darwin.sura.net!haven.umd.ed u!news.umbc.edu!nobody@ucbvax.Berkeley.EDU  (Mike Berman)
Subject: Re: simple help with Text_IO
Date: 3 Feb 93 18:21:45 GMT	[thread overview]
Message-ID: <1kp2fpINNcu4@umbc4.umbc.edu> (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

             reply	other threads:[~1993-02-03 18:21 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-02-03 18:21 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!darwin.sura.net!haven.umd.ed [this message]
  -- strict thread matches above, loose matches on Subject: below --
1993-02-04 18:39 simple help with Text_IO agate!spool.mu.edu!uwm.edu!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu
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 14:14 agate!spool.mu.edu!howland.reston.ans.net!bogus.sura.net!darwin.sura.net!
1993-02-03  1:47 agate!spool.mu.edu!sdd.hp.com!elroy.jpl.nasa.gov!usc.edu!cs.utexas.edu!to
replies disabled

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