Lazarus

Free Pascal => Beginners => Topic started by: Raffyn on May 16, 2021, 09:50:52 pm

Title: type [SOLVED]
Post by: Raffyn on May 16, 2021, 09:50:52 pm
Hello, I have a problem with my code and don't know why it isn't working or at least I don't see the mistake.
Code: Pascal  [Select][+][-]
  1. program VerbundFeldKombi;
  2. uses crt;
  3. type person = record
  4.              n: string;
  5.              a: string;
  6.              t: string;
  7.      end;
  8. type feld = array[1..30] of person;
  9. var ds:feld;
  10.     i, j, z: integer;
  11.     temp: person;
  12.  
  13. begin
  14.  clrscr;
  15.  {Eingabe}
  16.  write('Anzahl der einzugebeneden Datensaetze: ');
  17.  read(z);
  18.  
  19.  for i:= 1 to z do
  20.   with ds[i] do
  21.    begin
  22.     writeln('Name, Vorname eingeben: ');
  23.     readln(n);
  24.     writeln('Plz, Ort, Str., Nr. eingeben: ');
  25.     readln(a);
  26.     writeln('Telefonnr. eingeben: ');
  27.     readln(t);
  28.    end;
  29. clrscr;
  30. {Sortieren}
  31.  
  32. for i:= 1 to z-1 do
  33.  for j:=z downto i do
  34.   with ds[i] do
  35.    if ds[i].n > ds[j].n then
  36.     begin
  37.      temp:=ds[i];
  38.      ds[i]:=ds[j];
  39.      ds[j]:=temp;
  40.     end;
  41. {Anzeigen}
  42. for i:= 1 to z do
  43.  with ds[i] do
  44.   begin
  45.    writeln('Name, Vorname: ', n);
  46.    writeln('Anschrift: ', a);
  47.    writeln('Telefon: ',t);
  48.   end;
  49.  readkey;
  50. end.

It's supposed to insert first information then second then third but when runs it shows both the first and second request at once.
Thank you Raffy
Title: Re: type
Post by: Bart on May 16, 2021, 10:36:30 pm
Change read(z) into readln(z), this will "consume" the Enter key.

B.t.w: the topic title ("type") isn't very helpfull.
A good topic title describes the proble you are seeking help for e.g.
"How to sotr an array?"
"Why does this piece of code crash?"

Bart
Title: Re: type
Post by: MarkMLl on May 16, 2021, 11:11:30 pm
B.t.w: the topic title ("type") isn't very helpfull.

But he gets credit for putting his failing code in a code box, and giving the community a complete program which can be checked easily.

MarkMLl
Title: Re: type
Post by: pascal111 on May 16, 2021, 11:12:39 pm
أعتقدُ أنّي قرأتُ مرّةً في الـ Help الخاص بالـ Turbo Pascal أنّ read تختلف عن readln في أنّها لا تُضيف لبنة نهاية السطر (تغذية السطر) وأنّ ذلكـ يُحدث مُشكلةً في عبارات الـ readln التالية إذا كانت القراءة من الـ console أي تقريباً لا يُفضّل إستخدام read في القراءة من الـ console.

google translate:

"I think I once read in the Help for "of" Turbo Pascal that read differs from readln in that it does not add an end-of-line block "character" (line feed) and that this creates a problem in the following readln statements if reading from the console i.e. almost not preferring to use read for reading, From "from" the console."
Title: Re: type
Post by: lucamar on May 17, 2021, 12:49:54 am
I think I once read in the Help for "of" Turbo Pascal that read differs from readln in that it does not add an end-of-line block "character" (line feed) and that this creates a problem in the following readln statements if reading from the console i.e. almost not preferring to use read for reading, "from" the console.

It isn't that it doesn't "add" an EOL but that it reads up to but excluding the ending <CR> (or <LF>), while ReadLn reads and discards it. The consequence is that after a Read there is a <CR> still in the queue which a later ReadLn will interpret as reading an empty line.

There are several ways to deal with this, among other by using the EOLn function for a conditional ReadLn, but if all one's doing is just reading a single value it's usually better to just use ReadLn().
Title: Re: type
Post by: pascal111 on May 17, 2021, 01:05:12 am
I think I once read in the Help for "of" Turbo Pascal that read differs from readln in that it does not add an end-of-line block "character" (line feed) and that this creates a problem in the following readln statements if reading from the console i.e. almost not preferring to use read for reading, "from" the console.

It isn't that it doesn't "add" an EOL but that it reads up to but excluding the ending <CR> (or <LF>), while ReadLn reads and discards it. The consequence is that after a Read there is a <CR> still in the queue which a later ReadLn will interpret as reading an empty line.

There are several ways to deal with this, among other by using the EOLn function for a conditional ReadLn, but if all one's doing is just reading a single value it's usually better to just use ReadLn().

تصويبٌ ممتاز ومعلوماتٌ غنيّة أغبطكـ عليها.

لقد حاولتُ أنْ أكون عُضواً فاعلاً وأنْ أُساعد غيري ولكن يبدو أنّني أفتقد بعض الدقائق والتفاصيل ،مازلتُ أتعلّم.

google translate:

"Excellent shooting "correction" and rich info I envy (good kind of envy) you "about it".

I have tried to be an active member and help others but it seems that I am missing some minutes and details, I am still learning."
Title: Re: type
Post by: PascalDragon on May 17, 2021, 09:07:06 am
I have tried to be an active member and help others but it seems that I am missing some minutes and details, I am still learning."

And there's no fault in trying to be helpful even if one doesn't know the small details. :)

To be fair: this specific difference between Read and Readln isn't important very often as most people simply tend to use Readln. ;)
Title: Re: type
Post by: Nitorami on May 17, 2021, 10:13:02 am
Quote
this specific difference between Read and Readln isn't important very often as most people simply tend to use Readln.

Yes, this is what I am used to. But under windows 10 it does not seem to work as it used to.
When running this code in a windows 10 cmd shell from the textmode IDE (3.2.0), I need to press enter twice to arrive at the writeln; and writeln overwrites the number I typed.
I changed to "use legacy console" but the behaviour remains the same.

Code: Pascal  [Select][+][-]
  1. var x: single;
  2.  
  3. begin
  4.   repeat
  5.     readln (x);
  6.     writeln(x);
  7.   until x=0;
  8. end.
  9.  
Title: Re: type
Post by: pascal111 on May 17, 2021, 01:43:25 pm
I have tried to be an active member and help others but it seems that I am missing some minutes and details, I am still learning."

And there's no fault in trying to be helpful even if one doesn't know the small details. :)

To be fair: this specific difference between Read and Readln isn't important very often as most people simply tend to use Readln. ;)

رائعٌ هذا التشجيع ،أشكركـ.

google translate:

"Great encouragement, thank you."
TinyPortal © 2005-2018