Forum > Beginners

Question about a space being generated after subject in a record

(1/1)

Septe:
Hello,

I was following a tutorial that I dug up online.  I entered in an example code and kept getting a newline after printing the subject and not sure what to fix.

Here's the code:

--- Code: ---program exRecords;
uses crt;
type
  Books = record
    title : packed array[1..50] of char;
    author : packed array[1..50] of char;
    subject : packed array[1..100] of char;
    book_id : longint;
  end;

  var
    Book1, Book2 : Books; {Declare book1 and book2 of type Books}

begin
  {book1 specification}
  Book1.title := 'C Programming';
  Book1.author := 'Nuha Ali';
  Book1.subject := 'C Programming Tutorial';
  Book1.book_id := 6495407;

  {book2 specification}
  Book2.title := 'Telecom Billing';
  Book2.author := 'Zara Ali';
  Book2.subject := 'Telecom Billing Tutorial';
  Book2.book_id := 6495700;

  {print book1 info}
  writeln('Book 1 title: ', Book1.title);
  writeln('Book 1 author: ', Book1.author);
  writeln('Book 1 subject: ', Book1.subject);
  writeln('Book 1 book id: ', Book1.book_id);

  writeln;

  {print book2 info}
  writeln('Book 2 title: ', Book2.title);
  writeln('Book 2 author: ', Book2.author);
  writeln('Book 2 subject: ', Book2.subject);
  writeln('Book 2 book id: ', Book2.book_id);
  readkey;
end.

--- End code ---

I'm running this on windows 7 64 bit using Lazarus 1.4.
Than ks,
Septe

Septe:
This is the output that would be generated:

C Programming
Nuha Ali
C Programming Tutorial
                                          <---This is unexpected
6495407
                                          <---This is expected
Telecom Billing
Zara Ali
Telecom Tutorial
                                          <---This is unexpected
6495700

I hope this clarifies what I'm seeing.  I don't know why the little program is doing this.

Thanks

Basile B.:
By default the console width is 80 chars.

the followings variables fit in a single line:

--- Code: ---title : packed array[1..50] of char;
author : packed array[1..50] of char;

--- End code ---

but not this one:


--- Code: ---subject : packed array[1..100] of char;

--- End code ---

This explains why you have a blank line after writing the subject. Actually it's not blank. it 20 times a null char (#0).

The type packed array[1..100] of char is initialized with 100 null chars. If you want to write only the valid chars, you have to cast it as a null terminated string:


--- Code: ---writeln('Book 1 subject: ', Pchar(@Book1.subject[1]));

--- End code ---

But the tuto you've found is actually obsolete (Probably written 20 years ago !). Since a long time Pascal has dynamic arrays. You can rewrite your record:


--- Code: --- 
Books = record
    title : string;
    author : string;
    subject : string;
    book_id : longint;
  end; 

--- End code ---

And then you won't have to cast the array of char. However, if in the next tutos you have to write file of records, just keep them a static array.

Septe:
Ok this was related to the other question I had and it didn't occur to me about that.  Thanks for taking the time to let me know.  I appreciate it. :)

Navigation

[0] Message Index

Go to full version