Hello, I have been asked to create a pascal program but I am finding it really difficult because I haven't dealt with text files or binary searches before.

The program is supposed to read a Student ID and then the program should display the Student's details. All of the data will be stored on a text file. I'm guessing an array will have to be used to store data of the text file?

The text file contains data like this:

1 Angelica Adams
2 Lloyd Flanders
3 Richard Smith

If someone wanted to know who the third person to apply to a course was. they would need to enter "3" and "Richard Smith" should appear.

The program will eventually have 50 entries/records.

I found something on binary searches but cannot use it because I don't know how to assign lines of the text files to different elements of the array. I'm probably doing it all wrong but here's the code I have got so far:

Program StudentRegister;
Uses
    WinCrt;
Var
    f: Text;
    i: Integer;
    StringArray: Array[0..50] of String;
    ArrayLength: Integer;
    Low: Integer;
    Mid: Integer;
    High: Integer;
    Result: Integer;
    StudentID: String;
Label
    Break;
Begin
    Assign(f, 'filename.txt');
    reset(f);
    i := 0;
    while not eof(f) do
    begin
        readln(f, StringArray[i]);

        write('Enter the Student ID: ');
        readln(StudentID);

        i := i + 1;
        Low := 0;
        High := Length(StringArray[i])-1;
        Result := -1;
        if StringArray[Low] = StudentID then
            Result := Low
        else if StringArray[High] = StudentID then
            Result := High
        else while (Low <> High) and (Low+1 <> High) do
        begin
            Mid := (High-Low) div 2 + Low;
            if StringArray[Mid] = StudentID then
            begin
                Result := Mid;
                Goto Break
            end else if StringArray[Mid] > StudentID then
                High := Mid
            else
                Low := Mid;
            end;
            if StringArray[Low] = StudentID then
                Result := Low
            else if StringArray[High] = StudentID then
                Result := High;
            if Result = -1 then
                writeln('Student No.', StudentID, ' does not exist.')
            else
                writeln(StudentID, ': ', Result);
    end;
    Break:    Close(f);
    ArrayLength := i;
End.

I think my binary search is wrong as "studentid does not exist" appears when I type any number below 2. If I enter numbers greater than 1, the program takes up 99% cpu usage and seems like it has frozen for a short while. If someone can point out what I'm doing wrong, that would be useful.

Any help would be greatly appreciated. Thank you.

okay reading that(well trying to read that) gave me a headache lol,anyways i jst started wiv txt file manipulation and stuff so no idea here.. !
got a couple of pascal tutorial links if you want :)

bdw 99% cpu usage is probably cuz a loop is running,happens with while loop when da counter variable value is not defined before da loop ;)
p.s sorry for da double post mods,can't really bother editing my other post wiv this lightning fast connectio i got :S

bdw 99% cpu usage is probably cuz a loop is running,happens with while loop when da counter variable value is not defined before da loop ;)
p.s sorry for da double post mods,can't really bother editing my other post wiv this lightning fast connectio i got :S

That's what I thought too but I couldn't use the Break command because my compilor does not support it. Instead I have used goto but it hasn't made a difference. If those links on file manipulation involve using binary searches (and preferably text files), could you send them to me please.

Sorry about the headache

1. Is your datafile arranged in and ordered sequewnce? The binary search begins generally as you have, finding the high , low and middle values. The student ID must be evaluated against the middle value to determine if it is higher or lower. ONce that is determined either the low value is changed to the middle (student ID is greater than the middl) or high is changed to Middle (student value was less than middle). This loop is continued until the student value matches the middle value.

2. I do not recommend using break. Create a boolean variable called found and set it to false and then create a loop like this:

found := false;
Repeat
(loop code here. found := true when mid = studentID)
Until found;

3. When reading a text file, you may have to create a separate function to evaluate it if the contents of each text line contains text other than the actual ID.

went through my book but it only has a single page on binary searching and that too in pseudocode !
wdh ima fight my headache and type that code in lol..use enter once in a while will ya :P

I'm not using a Break! I'm using a Goto because my compilor does not support Break. I did find an error in my code - I should have read all the lines of the file into array elements and then use a search.

Still having problems getting it to work. My text file hasn't changed. Still only 3 entries like it is stated on my first post.

My bad, I meant Goto, but used your label name instead.

Can you show a sample of your text file lines? Are they in order? You are right, your array must be loaded and in order before you begin the binary search. You might want to break your code up into several procedures to handle the various tasks. This will be more important if this program were to be expanded or used as a future resource for future programs. For instance, one proceudre could handle the files, One could load and sort the array, one could evaluate whether one line is higher, lower or equal to another, and a final one could handle the program.

Program StudentRegister

Uses...
Var...
done : boolean;

Procedure GetMyFile...
Procedure LoadSortArray...
Procedure GetHighLow...
Procedure PromptforID...

Begin
  done := false;
  getmyFile;
  loadSortArray;
  Repeat
    PromptforID;
  Until done;
End.

nevermind. I have found a solution. The binary search was wrong. I am now using this binary search:

while high >= low do
  begin
    i := (low + high) div 2;
    key := copy(StudentRecords[i], 1, 2);
    if StudentID < Key then
    begin
      {increment high}
    end
    else if StudentID > Key then
    begin
      {decrement low}     
    end
    else
    begin
      writeln(StudentRecords[i]);
      halt;
    end;
  end;
This question has already been answered. Start a new discussion instead.