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


Try reading book.title (or setting book.title:=titlein)  and then writing
the book  record, not the titlein string.   If the file must pre-exist, then
I guess you want the errorcode test to be for 0 and then maybe use append
and not rewrite to open it - otherwise you'll never get more than one book
in a file.

_________________________
Gary
http://www.delphiforfun.org
_________________________

Quote
"Heero Yuy" <blade16...@canada.com> wrote in message

news:JXKQ5.127465$47.1987332@news.bc.tac.net...
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);

> 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


Try use readln (instead of read) when reading user input.
Else you will have an Enter key staying in the input buffer at the next
readln.

Christen

Quote
----- Original Message -----
From: "Heero Yuy" <blade16...@canada.com>

Newsgroups: comp.lang.pascal.misc
Sent: Thursday, November 16, 2000 7:15 AM
Subject: what am I doing wrong

> begin
> writeln('please enter file name');
> read(infile);
> assign(bookfile,infile);

Other Threads