Board index » delphi » runtime error/IOE in TPW 1.5 under Win98

runtime error/IOE in TPW 1.5 under Win98

I don't know what is wrong with this program:

****************************************************
program BinarySearch (InFile, output);

const
    Low = 1;
    High = 9;

type
    NumArray = array[Low..High] of integer;

var
        Arr        :NumArray;
        SearchItem, First, Last, TargetIndex, i :integer;
    InFile     :Text;

procedure InitArr(var A :NumArray; var InFile :Text);

var
    i :integer;

begin{InitArr}
    Assign(InFile, 'Textfile.txt');
        Reset(InFile);
        while not EOF(InFile) do
                begin
                        readln(Infile, A[i]);
            {writeln(A[i]);}
                        i := i + 1
        end
end;{InitArr}

function BinSearch(A :NumArray; Target :integer;
                   First, Last :integer) :integer;
{
This function performs a recursive binary search of an ordered
array with subscripts First..Last. If the search is successful,
the function returns the subscript of Target. Otherwise, the
value of 0 is returned to signify an unsuccessful search.

Quote
}

var
        Mid :integer;

begin{BinSearch}
        Mid := (First + Last) div 2;

{    writeln('mid:', Mid, ' first: ', First, ' last: ', Last);
    writeln(A[Mid]);
    }

        if First > Last then {a stopping case - target not found}
                BinSearch := 0
        else
                if Target = A[Mid] then {another stopping case - target found}
                        BinSearch := Mid
                else
                        if Target < A[Mid] then
                                BinSearch := BinSearch(A, Target, First, Mid - 1)
                        else
                                BinSearch := BinSearch(A, Target, Mid + 1, Last)
end;{BinSearch}

begin{main}
        InitArr(Arr, InFile);
        write('Enter what you''re looking for > ');
    readln(SearchItem);
        First := Low;
    Last := High;

{for i := first to last do
        writeln(arr[i]);  }

        TargetIndex := BinSearch(Arr, SearchItem, First, Last);
        writeln;
        if TargetIndex = 0 then
                writeln('Sorry, you still haven''t found what you''re looking for')
        else
                writeln('Your search result > ', Arr[TargetIndex]);
    readln
end.{main}
***************************************
Please help.....thanks in advance

 

Re:runtime error/IOE in TPW 1.5 under Win98


Not putting where your runtime error is makes it a little difficult, but one
thing that might be causing it is that you are not initializing i before you
read into your array in your procedure InitArr.

Rick

Quote
adrian lai wrote in message <35F28F93.9266E...@tm.net.my>...
>I don't know what is wrong with this program:
>procedure InitArr(var A :NumArray; var InFile :Text);

>var
>    i :integer;

>begin{InitArr}
>    Assign(InFile, 'Textfile.txt');
> Reset(InFile);
> while not EOF(InFile) do
> begin
> readln(Infile, A[i]);
>            {writeln(A[i]);}
> i := i + 1
>        end
>end;{InitArr}

Other Threads