From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 3 Feb 93 18:21:45 GMT 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 Message-ID: <1kp2fpINNcu4@umbc4.umbc.edu> List-Id: 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