1

I am trying to write a program to read a long list of book(1000 books),isbn etc but when the program runs, it shows range overrun the format of txt is

1
1234567890
ABC book
peter
20
2
1234567896
...

the code is:

const maxbk=1000;
type bookrecord = record
                    book_no:string;
                    isbn:string;
                    book_name:string;
                    author:string;
                    borrowed:string;
                    end;
var booklist : array[1..maxbk] of bookrecord;totalbook:integer;

procedure readbooklist(var bklist:array of bookrecord;var totalbk:integer);
var f:text;temp:string;code:integer;
begin
  totalbk:=0;
  assign(f,'bklist.txt');
  reset(f);
  while not eof(f) do
  begin
    readln(f,bklist[totalbk+1].book_no);
    readln(f,bklist[totalbk+1].isbn);
    readln(f,bklist[totalbk+1].book_name);
    readln(f,bklist[totalbk+1].author);
    readln(f,bklist[totalbk+1].borrowed);
    totalbk:=totalbk+1;
  end;
  close(f);
  writeln('read file done');
end;

begin
  readbooklist(booklist,totalbook);
end.

who can help to fix the problem??

2
  • to Joachim Isaksson, thx for answering, but I allocated 1000 array which I think i can assume that I can at least put 999 books there but my list actually only contains info of 600 books Commented Aug 6, 2013 at 11:44
  • What is the exact error message you're getting? "it shows range overrun" means nothing, because we can't see your screen from where we are, and you have that information and can see it. Please edit your question to include it. If you provide specific details, it helps you get help faster. Commented Aug 7, 2013 at 2:08

1 Answer 1

1

I think the problem is in your handling of the array parameter. Try this (highlighted in bold are the changes I've added):

const maxbk=1000;
type bookrecord = record
                    book_no:string;
                    isbn:string;
                    book_name:string;
                    author:string;
                    borrowed:string;
                    end;

var booklist : array[1..maxbk] of bookrecord; totalbook:integer;

procedure readbooklist(var bklist:array of bookrecord;var totalbk:integer);
var f:text;temp:string;code:integer;
begin
  totalbk:=Low(bklist);
  assign(f,'bklist.txt');
  reset(f);
  while not eof(f) do
  begin
    readln(f,bklist[totalbk].book_no);
    readln(f,bklist[totalbk].isbn);
    readln(f,bklist[totalbk].book_name);
    readln(f,bklist[totalbk].author);
    readln(f,bklist[totalbk].borrowed);
    totalbk:=totalbk+1;
  end;
  totalbk := totalbk - Low(bklist);
  close(f);
  writeln('read file done');
end;

begin
  readbooklist(booklist,totalbook);
end.

Also, a few choice spaces would help with readability (like a space after each comma and around assignment operators).

Note, too, that your code (and the changed code I'm providing) don't check for incomplete records in your input text file or properly check for blank lines, etc (e.g., invalid book_no values). You should attempt to add some code which makes it a little more resilient to problems in the input file. As others have pointed out, there are probably better ways to structure the input and read it as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.