Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!elroy.jpl.nasa.gov!usc!apple!sun-barr!newstop!jethro!Sun.COM!acm From: acm@Sun.COM (Andrew MacRae) Newsgroups: comp.lang.pascal Subject: Re: Overflow error Message-ID: <4248@jethro.Corp.Sun.COM> Date: 31 May 91 14:43:52 GMT References: <675648024.0@nwark.FidoNet> Sender: news@jethro.Corp.Sun.COM Reply-To: acm@Sun.COM (Andrew MacRae) Organization: Sun Microsystems, Mountain View CA Lines: 96 Cc: acm In article <675648024.0@nwark.FidoNet>, Alex.Brown@f1020.n391.z1.FidoNet.Org (Alex Brown) writes: > > I am getting an overflow error after 3 pages are displayed. Can > any tell > why and how to avoid it in the future. The problem is that you are calling the procedure getstring recursivly (that is from within the procedure itself). Rather than have that call to getstring at the end of getstring, try using (yet another) WHILE loop testing for eof outside the scope of the loop that is testing for eof and f < 25. The reason I say 'yet another WHILE loop' is that you should learn how to use the FOR loop. Your code in the procedure showpage that sets f to 1, then loops while incrementing f and testing for completion is in essence a FOR loop. You are also going to run into problems of an interesting nature when displaying the last page of a file, but I will let you discover how. (It's a problem I remember running into myself.) Andrew MacRae > > The program is supposed to grab 24 lines of a text and display them > with a > >>>>>>>MORE<<<<<<<< message at the bottom. > > > Program View; > uses dos, crt; > > var > ftext : text; > f : integer; > s : array[1..25] of string; > > procedure showpage; > begin > f := 1; > while f < 25 do > begin > writeln(s[f]); > inc(f); > end; > end; > > > procedure getstring; > begin > f := 1; > while (not eof(ftext)) and (f < 25) do > begin > readln(ftext, s[f]); > inc(f); > end; > if not eof(ftext) then > begin > s[25] := ' >>>>>>>>MORE<<<<<<<< '; > showpage; > readln; > end; > getstring; > end; SUGGESTED: Procedure getstring; Begin While not EOF(ftext) do Begin f := 1; While (not EOF(ftext)) and (f < 25) do Begin ReadLn(ftext, s[f]); Inc(f) End; If not Eof(ftext) Then Begin s[25] := ' >>>>>>>>MORE<<<<<<<< '; showpage; ReadLn End End End; > > begin > if paramcount <> 1 then > begin > writeln('Syntax is VIEW "filename" '); > halt; > end; > assign(ftext,paramstr(1)); > > reset(ftext); > getstring; > close(ftext); > end.