Article View: comp.lang.pascal.borland
Article #649Re: WHAT COULD BE WRONG?
From: Huub van Dooren
Date: Thu, 18 May 2000 00:00
Date: Thu, 18 May 2000 00:00
176 lines
5970 bytes
5970 bytes
Here are some (maybe usefull) notes on your program.
1. Do not use the word _recursive_ when you are talking about a
loop. Recursive procedures are procedures where the outcome
depends on a previous call to that procedure.
Examples: Fibonacci numbers and fractals or scanning a
directory-tree (= each sub-directory can have zero or
more subdirectories, wich can be treated the same way as the
current sub-directory). Since the outcome of the previous found
word is not of influence to the current 'to be found' word, you
can
not speak of recursion. Use the word repetition (or iteration)
instead.
2. Try to use "exit" and "halt" as less as possible.
Why? When a procedure or a program has more ways to skip code
(=exit) or end (=halt), the readability of it becomes lost. The
proper
use of "if...then..." can prevent this. Make sure a procedure or
program has only one exit-point, namely at the end of it.
3. Declare variables in the procedure where they are used in. Try
to declare as less global variables as you can. These global var
need not to bother procedures where they have no use. It makes
the program also more readable (and you can save some memory).
Regard a procedure as a little program in itself.
Tips:
- Using a repeat...until EOF on an empty file means trouble.
- Never use a semicolon (;) before the else-, end- or
until-keyword
- Try to make a procedure do only one (simple) thing
- When in trouble with loops: place the inside of it in a
separate procedure.
- Use different names for procedure-parameters and variables that
have the same meaning. This also avoids confusion.
- Initialise: before doing anything with a variable, make sure it
has a legal and sensible value.
Keep up the good work. You're doing fine.
See if the following example makes some sense to you.
(Not tested, just practising)
Greetings. Huub.
program word_list_example(input,output);
var no_more:boolean;
function the_same(ss,tt:string):boolean;
begin
the_same:=(ss=tt)
end; {the_same}
procedure display_found(ff:boolean; ss:string);
{
input: ff - true=>ss is found in file, ss - word to be found
output: none
}
begin
write(ss);
if not ff
then write(' not ');
writeln('found.')
end; {display_found}
procedure search_this_word(s:string);
{input: s-word to be found, output:none}
var f:text; t:string;
found:boolean
begin
found:=false;
assign(f,'wordlist.dat');
reset(f);
while ((not eof(f)) and (not found)) do
(* = while not (eof(f) or found) do *)
begin
readln(f,t);
found:=the_same(s,t)
end;
close(f);
display_found(found,s)
end; {search_this_word}
procedure give_the_word(var stop:boolean);
{input,output: stop = stop lookup words}
var find_me:string;
begin
writeln('Give word to be found, <enter> to stop');
readln(s);
if s=''
then stop:=true
else search_this_word(find_me)
end; {give_the_word}
{main}
begin
no_more:=false;
repeat
give_the_word(no_more)
(* note 1*)
until no_more
end. {main}
(*
note1: actually "search_the_word" and "display_found"
should be placed here for better readability.
Preceded by 'if not no_more then begin'.
Variables "s" and "found" would then become
output-parameters to their successors.
To avoid all this complexity, maybe it would be better
to declare them global.
It comes to mind that you may not yet have had a
proper introduction to procedure-parameters in
your course. Sorry, my mistake.
*)
br-pede@online.no heeft geschreven in bericht ...
>
>
>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.
>
>Hope anybody out there could help. It will be greatly
appriciated.
>
>regards,
>Brian
>times}
>Net-Tamer V 1.11 - Test Drive
Technical Details
Message-ID:
<8g100h$hj5$1@news.IAEhv.nl>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: 111659,1d4aea8798c741f5 X-Google-Attributes: gid111659,public X-Google-Thread: 1097b9,1d4aea8798c741f5 X-Google-Attributes: gid1097b9,public From: "Huub van Dooren" <hvdooren@iae.nl> Subject: Re: WHAT COULD BE WRONG? Date: 2000/05/18 Message-ID: <8g100h$hj5$1@news.IAEhv.nl> X-Deja-AN: 625007277 Content-Transfer-Encoding: 7bit References: <D0OU4.6707$C9.118410@news1.online.no> Content-Type: text/plain; charset="iso-8859-1" X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 Organization: MyLittleShack, Eersel, NL Mime-Version: 1.0 Newsgroups: comp.lang.pascal.borland,dk.edb.programmering.pascal