We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,001 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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.
2
Contributors
2
Replies
3 Days
Discussion Span
2 Years Ago
Last Updated
3
Views
Question
Answered
wiredtoawall
Newbie Poster
4 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
6,301 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26

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
Skill Endorsements: 0
Question Answered as of 2 Years Ago by pyTony

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0751 seconds using 2.65MB