Board index » delphi » What am I doing wrong

What am I doing wrong

Can someone please tell me what I am doing wrong.
I am trying to get this program to have input from a user, then write that
input to a binary file.
If someone can help me thanks.
Program Assign4;

{}
uses CRT;
Type bookin=RECORD
     title:string[30];
     AuthorLastName:string[15];
     AuthorFirstName:string[15];
     Publisher:string[25];
     ISBN:string[13];
end;

bookfiletype=FILE of bookin;

var
   bookfile:bookfiletype;
   book:bookin;

procedure addrec;
var
infile:string;
titlein:string;
errorcode:integer;

begin
writeln('please enter file name');
read(infile);
assign(bookfile,infile);
{$I-}
reset(bookfile);
Errorcode:=IORESULT;
{$I+}
if errorcode = 2 then
begin
rewrite(bookfile);
write('Please enter book title');
readln(titlein);
write(bookfile,titlein);
end
else
    begin
         writeln('File ', infile,' not found');
         writeln('Please enter correct filename');
         write('Press ENTER to continue');
         readln;
    end;

write('Please enter book title');
readln(titlein);
write(bookfile,titlein);

end;

procedure menu;

var
option:char;

begin
repeat
clrscr;
write('A');
writeln(' Add a new book to inventory');
write('D');
writeln(' Delete a book from inventory');
write('F');
writeln(' Find and display all books by specified author');
write('L');
writeln(' List entire inventory');
write('Q');
writeln(' Quit');
writeln('Please enter an option:  ');
readln(option);
case option of

            'A','a': begin
                     addrec;
                     end;
            'D','d': begin
                     {deleterec;}
                     end;
            'F','f': begin
                     {findndisplay;}
                     end;
            'L','l': begin
                     {listin}
                     end;

end;
until (option='Q') or (option='q');
end;

begin
menu
end.

 

Re:What am I doing wrong


In article <pYKQ5.127466$47.1987...@news.bc.tac.net>,
  "Heero Yuy" <blade16...@canada.com> wrote:

Quote
> Can someone please tell me what I am doing wrong.
> I am trying to get this program to have input from a user, then write
that
> input to a binary file.
> If someone can help me thanks.
> Program Assign4;

> {}
> uses CRT;
> Type bookin=RECORD
>      title:string[30];
>      AuthorLastName:string[15];
>      AuthorFirstName:string[15];
>      Publisher:string[25];
>      ISBN:string[13];
> end;

> bookfiletype=FILE of bookin;

> var
>    bookfile:bookfiletype;
>    book:bookin;

> procedure addrec;
> var
> infile:string;
> titlein:string;
> errorcode:integer;

> begin
> writeln('please enter file name');
> read(infile);
> assign(bookfile,infile);
> {$I-}
> reset(bookfile);
> Errorcode:=IORESULT;
> {$I+}
> if errorcode = 2 then
> begin
> rewrite(bookfile);
> write('Please enter book title');
> readln(titlein);
> write(bookfile,titlein);
> end
> else
>     begin
>          writeln('File ', infile,' not found');
>          writeln('Please enter correct filename');
>          write('Press ENTER to continue');
>          readln;
>     end;

> write('Please enter book title');
> readln(titlein);
> write(bookfile,titlein);

You should read the details into the record (ie. book):

readln(book.title);

and then write the 'book' record to bookfile:

write(bookfile,book);

--
Iain Jones         xh0u9...@NOSPAMliv.ac.uk
Faculty Of Science, University Of Liverpool
http://www.liv.ac.uk

Sent via Deja.com http://www.deja.com/
Before you buy.

Re:What am I doing wrong


Quote
Heero Yuy wrote:

> Can someone please tell me what I am doing wrong.
> I am trying to get this program to have input from a user, then write that
> input to a binary file.
> If someone can help me thanks.

Your basic problem is that you can only write records of the
given type to your record file - more below.

Quote
> Program Assign4;

> {}
> uses CRT;
> Type bookin=RECORD
>      title:string[30];
>      AuthorLastName:string[15];
>      AuthorFirstName:string[15];
>      Publisher:string[25];
>      ISBN:string[13];
> end;

> bookfiletype=FILE of bookin;

> var
>    bookfile:bookfiletype;
>    book:bookin;

> procedure addrec;
> var
> infile:string;
> titlein:string;
> errorcode:integer;

> begin
> writeln('please enter file name');
> read(infile);
> assign(bookfile,infile);
> {$I-}
> reset(bookfile);
> Errorcode:=IORESULT;
> {$I+}
> if errorcode = 2 then

I don't remember the meaning of IOResult=2, but usually
IOResult=0 means that everything went well. More knowledgeable
people will surely comment on this, if I made a mistake.

Quote
> begin
> rewrite(bookfile);
> write('Please enter book title');
> readln(titlein);
> write(bookfile,titlein);

Error here:
A write statement to 'bookfile' expects a variable of type 'bookin'.
Here you are trying to write a variable of the type 'string'. These
two types are not compatible. A file of type 'text' would accept
strings. You have to decide which type of data you want store in
the files.

You should declare a local variable of type 'bookin', modify
its fields one at a time and after that write the entire record
to your 'bookfile'.

I recommend that you review everything your teacher said about typed
files, and that you reread your course material on this - there might
even be a helpful example somewhere.

Quote
> end
> else
>     begin
>          writeln('File ', infile,' not found');
>          writeln('Please enter correct filename');
>          write('Press ENTER to continue');
>          readln;
>     end;

> write('Please enter book title');
> readln(titlein);
> write(bookfile,titlein);

> end;

[snipped the rest of your code}

--
Jyrki Lahtonen, Ph.D.
Department of Mathematics,
University of Turku,
FIN-20014 Turku, Finland

http://users.utu.fi/lahtonen

Other Threads