954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help saving files in Pascal

I cannot read the info I stored into the files and I need help. Whenever I give the user the option of saving more records or simply retrieving a record, it always stops on the retrieving part.

Type
    Str25    = String[25];
    TBookRec = Record
                Title, Author,
                ISBN  : Str25;
                Price : Real;
               End;

Procedure EnterNewBook(var newBook : TBookRec);
Begin
 Writeln('Please enter the book details: ');
 Write('Book Name: ');
 Readln(newBook.Title);
 Write('Author: ');
 Readln(newBook.Author);
 Write('ISBN: ');
 Readln(newBook.ISBN);
 Write('Price: ');
 Readln(newBook.Price);
End;

Var
    bookRecArray : Array[1..10] of TBookRec;
    tempBookRec  : TBookRec;
    bookRecFile  : File of TBookRec;
    i            : 1..10;
    choice: integer;

Begin

writeln('Press 1 to record more books, Press 2 to looks at records');
readln(choice);
if choice=1 then
 begin
 Assign(bookRecFile, 'bookrec.dat');
 ReWrite(bookRecFile);
 For i := 1 to 10 do
  Begin
   EnterNewBook(bookRecArray[i]);
   { bookRecArray[i] now contains the book details }
   Write(bookRecFile, bookRecArray[i]);
  End;
 Close(bookRecFile);
 Writeln('Thanks for entering the book details.');
 Writeln('They are saved in a file!');
 end;

 if choice=2 then
  begin
 Write('Now choose a record to display from 1 to 10: ');
 Readln(i);
 ReSet(bookRecFile);
 Seek(bookRecFile, i-1);
 Read(bookRecFile, tempBookRec);
 Close(bookRecFile);
 Writeln('Here are the book details of record #',i,':');
 Writeln;
 Writeln('Title:  ', tempBookRec.Title);
 Writeln('Author: ', tempBookRec.Author);
 Writeln('ISBN:   ', tempBookRec.ISBN);
 Writeln('Price:  ', tempBookRec.Price);
 end;
 Readln;
End.
wiredtoawall
Newbie Poster
4 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

You Assign the file only if choice=1, so choice=2 crashes. It should be more obvious how many books must enter and which number is now.
No need to limit program to ten books. Better to search by title than ask number of book.

Program should repeat until user wants to quit. If there are few books only, good main menu includes list of all books or books fulfilling current search criteria, possibly paged, and prompt.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

You Assign the file only if choice=1, so choice=2 crashes. It should be more obvious how many books must enter and which number is now. No need to limit program to ten books. Better to search by title than ask number of book.

Program should repeat until user wants to quit. If there are few books only, good main menu includes list of all books or books fulfilling current search criteria, possibly paged, and prompt.


Alright thanks

wiredtoawall
Newbie Poster
4 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: