Article View: comp.lang.pascal.borland
Article #651Re: WHAT COULD BE WRONG?
From: Jeff Jones
Date: Thu, 18 May 2000 00:00
Date: Thu, 18 May 2000 00:00
91 lines
4126 bytes
4126 bytes
br-pede@online.no wrote:
>
> I am trying to make a software, where the user can put in some word, and the
> program will look and see if the word(s) is in a wordlist file. Everything
> is working pretty well, except this: The procedure is running in a recursive
> way, that if you want to look up more than one word, you only put it in next
> time you are asked. To exit this, you only have to input an empty string,
> that is to press the ENTER key. And here is the trouble. The program will
> not stop this looping, unless you enter several empty words.
>
> Here is the snip of the program that is causing trouble:
>
> procedure lookup;
> begin
> writeln;
> write('please enter word to be looked up: ');
> readln( s ); { s : STRING; }
> if s = '' then exit; { this doesn't work,
> unless running a couple of
> assign(f,'wordlist.dat'); reset(f);
> repeat
> read(f,t); { t : STRING }
> if t = s then found := true else found := false; {found : BOOLEAN}
> until eof(f);
> close(f); {f : FILE OF STRING}
> if not found then begin
> writeln('the word ', s, 'is not found in the list');
> lookup;
> end;{if}
> writeln('the word ', s, ' is found in the list');
> end;{ procedure lookup}
>
> Begin
> { call a menu}
> Repeat read(keychoice) until keychoice in [#0 .. #255];
> { keychoice : CHAR }
> if keychoice = '1' then lookup;
> { call the menu}
> ...
> end.
I don't see a need for "lookup" to call itself. Try the following:
procedure Lookup;
const WORDLIST_FILE = 'wordlist.dat';
begin
assign(f,WORDLIST_FILE);
reset(f);
if eof(f) then
writeln('Wordlist file ',WORDLIST_FILE,' is empty')
else
begin (* f is not empty *)
writeln;
write('Please enter word to be looked up or just Enter to
quit: ');
readln( s ); { s : STRING; }
repeat (* find the word and get new word to find until empty
string entered *)
reset(f); (* reset the wordlist file to do further
lookups *)
found := false;
repeat (* try to find the word *)
readln(f,t);
found := (t = s);
until found or eof(f);
write('The word ',s,' is ');
if not(found) then write('not ');
writeln('found in the list');
writeln;
write('Please enter word to be looked up or just Enter to
quit: ');
readln( s ); { s : STRING; }
until s = ''; (* find the word, get new word to find until
empty string entered *)
end; (* f is not empty *)
close(f);
end; (* Lookup *)
Ideally, you'd want some error-checking to make sure you could open the
wordlist file. I suspect that many variables like "found", "s", and "t"
don't need to be global.
Personally, I'd change some of the redundant code I posted above to be a
procedure declared inside "Lookup" as well as the repeat loop which
tries to find the matching word. I'd also make the variable names
longer and more descriptive of their usage.
Technical Details
Message-ID:
<39242B26.F5EE55F8@mail.sandi.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<D0OU4.6707$C9.118410@news1.online.no>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1097b9,1d4aea8798c741f5 X-Google-Attributes: gid1097b9,public X-Google-Thread: 111659,1d4aea8798c741f5 X-Google-Attributes: gid111659,public From: Jeff Jones <jjones9@mail.sandi.net> Subject: Re: WHAT COULD BE WRONG? Date: 2000/05/18 Message-ID: <39242B26.F5EE55F8@mail.sandi.net> X-Deja-AN: 625070688 Content-Transfer-Encoding: 7bit References: <D0OU4.6707$C9.118410@news1.online.no> X-Accept-Language: en,pdf X-Sender: "Jeff Jones" <@mail.sandi.net> (Unverified) Content-Type: text/plain; charset=us-ascii X-Complaints-To: abuse@sandi.net X-Trace: news.sandi.net 958671654 73022 165.24.2.194 (18 May 2000 17:40:54 GMT) Organization: San Diego City Schools Mime-Version: 1.0 NNTP-Posting-Date: 18 May 2000 17:40:54 GMT Newsgroups: comp.lang.pascal.borland,dk.edb.programmering.pascal