From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,cbb87dd49168c396 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-10-29 17:31:21 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!headwall.stanford.edu!newsfeed.news2me.com!newsfeed2.earthlink.net!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread1.prod.itd.earthlink.net.POSTED!not-for-mail Message-ID: <3DBF3659.30709@acm.org> From: Jeffrey Carter User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.0.0) Gecko/20020530 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Get_Line References: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Date: Wed, 30 Oct 2002 01:30:28 GMT NNTP-Posting-Host: 63.184.104.48 X-Complaints-To: abuse@earthlink.net X-Trace: newsread1.prod.itd.earthlink.net 1035941428 63.184.104.48 (Tue, 29 Oct 2002 17:30:28 PST) NNTP-Posting-Date: Tue, 29 Oct 2002 17:30:28 PST Organization: EarthLink Inc. -- http://www.EarthLink.net Xref: archiver1.google.com comp.lang.ada:30210 Date: 2002-10-30T01:30:28+00:00 List-Id: Justin Birtwell wrote: > Hi, > > I'm having some strange behavior with Get_Line. In a procedure called > GetData I call Get_Line to receive input from the command line. The first > time this function is called it behaves fine prompting the user for input. > The second time it runs the execution passes through Get_Line and no prompt > appears on the command line. Upon doing a little research in the Ref Man. > I found a statement that talks about if Get_Line finds a line terminator it > automatically returns. But how can this be? Here's the statement from the > RM. > > << A string read by Get or written by Put can extend over several lines. An > implementation is allowed to assume that certain external files do not > contain page terminators, in which case Get_Line and Skip_Line can return as > soon as a line terminator is read. >> > > But I'm not reading from a file? I'm reading from the command line? Does > anyone understand this behavior? > > Sincerely, > Justin > > P.s. Here's a few statements leading up to the problem... > > procedure Getdata(P_Num_Of_Points: out Natural) is > Last:Natural:=0; > Buffer:String(1..80):=(1..80=>Character'val(0)); > l_Num:Integer; > --Answer:Character; > begin > > --get input > --are there any invalid characters > --is it a valid number anyway? > --does the user want to use this number? > Ada.Text_Io.Put_Line("Enter between 1 and 6 Balloons to create in > the box. "); > loop > begin > > ---This line gets skipped > returning with an empty buffer!!!!!!! > if Jb.Stringfunctions.Is_Integer(Buffer(Buffer'First..Last)) then This hardly gives us enough information for a precise reply. There is no call to Get_Line here; even if there was, we don't have the information to reason about the environment in which it is operating (due to other I/O that preceeds the call). However, questions such as this arise often enough that we can make an educated guess. First, what you call the command line is a file called Standard_Input, so you are reading from a file. The Text_IO subprograms that do not take a File parameter read from Current_Input, which is Standard_Input unless you have changed it. Second, if Get_Line returns Last < Buffer'First, then you've read an empty line. I assume this is what you mean by "an empty buffer". There are a number of ways this can happen. If a previous call to Get_Line returns Last = Buffer'Last, then that call has not skipped a line terminator, and that line terminator is still there, waiting to be skipped, probably by your next call to Get_Line. If your input line is exactly Buffer'Length characters long, then all that's you've left is the line terminator, resulting in an empty line to read next time. Another source of empty lines is using Get from an instantion of one of the Ada.Text_IO generic packages such as Integer_IO. This do not skip a line terminator. They also cause other difficulties, so reading into a string first is a good ides. You can skip the next line terminator by calling Skip_Line. Another approach is to use a function (often called Get_Line) that reads an entire line and skips the line terminator, and returns a String: function Get_Line (File : Ada.Text_IO.File_Type := Ada.Text_IO.Current_Input) return String; This ensures that line terminators are always skipped. This is not something that comes with the language, so you'll either have to write your own or find one from somewhere else. One source is the PragmAda Reusable Components, which has such a function: http://home.earthlink.net/~jrcarter010/pragmarc.htm -- Jeff Carter "Your mother was a hamster and your father smelt of elderberries." Monty Python & the Holy Grail