Recent

Author Topic: Question about a space being generated after subject in a record  (Read 2933 times)

Septe

  • Jr. Member
  • **
  • Posts: 68
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: [Select]
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.

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

Septe

  • Jr. Member
  • **
  • Posts: 68
Re: Question about a space being generated after subject in a record
« Reply #1 on: April 24, 2015, 01:55:03 am »
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.

  • Guest
Re: Question about a space being generated after subject in a record
« Reply #2 on: April 24, 2015, 02:42:10 am »
By default the console width is 80 chars.

the followings variables fit in a single line:
Code: [Select]
title : packed array[1..50] of char;
author : packed array[1..50] of char;

but not this one:

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

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: [Select]
writeln('Book 1 subject: ', Pchar(@Book1.subject[1]));

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: [Select]
 
Books = record
    title : string;
    author : string;
    subject : string;
    book_id : longint;
  end; 

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.
« Last Edit: April 24, 2015, 02:48:14 am by Basile B. »

Septe

  • Jr. Member
  • **
  • Posts: 68
Re: Question about a space being generated after subject in a record
« Reply #3 on: April 24, 2015, 05:24:48 am »
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. :)


 

TinyPortal © 2005-2018