Recent

Author Topic: why skip read in for loop  (Read 3429 times)

NoReason

  • New Member
  • *
  • Posts: 21
why skip read in for loop
« on: November 28, 2017, 02:25:09 am »
program Project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes
  { you can add units after this };
  var
    a:string;
    b1:string;
    b:array [1..100] of string;
    i,c:integer;

begin
  writeln('Upisi ime programa');
  read(a);
  writeln('koliko reci posle imena programa zelis da imas?');
  read(c);

  for i:=1 to c do
  begin
    writeln('Upisi ',i,' rec');
    read(b[i ]); (this)
   

  end;
  writeln;
  writeln('Ime programa je:',a);
  writeln;
  writeln('Niz reci posle imena programa su:');
  for i:=1 to c do
  begin
    writeln(b[i ]);

  end;


   read(c);

end.
     
« Last Edit: November 28, 2017, 02:28:10 am by NoReason »

jamie

  • Hero Member
  • *****
  • Posts: 6128
Re: why skip read in for loop
« Reply #1 on: November 28, 2017, 04:09:24 am »
For strings "READ" will return immediately if there are no Keys in the keyboard buffer or,  it will return
with only what was in the buffer before you called "READ"

"ReadLn" will wait for you to enter some data.
The only true wisdom is knowing you know nothing

NoReason

  • New Member
  • *
  • Posts: 21
Re: why skip read in for loop
« Reply #2 on: November 28, 2017, 07:26:18 am »
Thanks, but got another problem... How to compare 2 strings by ASCII? CompareText or CompareStr wont work :/

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: why skip read in for loop
« Reply #3 on: November 28, 2017, 08:04:43 am »
Thanks, but got another problem... How to compare 2 strings by ASCII? CompareText or CompareStr wont work :/
They work as expected. At least I get proper results out of them provide a small sample which demonstrate your problem.

For strings "READ" will return immediately if there are no Keys in the keyboard buffer or,  it will return
with only what was in the buffer before you called "READ"

"ReadLn" will wait for you to enter some data.

No sorry its the other way around. read will pause the program execution until there is something to read from the keyboard buffer.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

NoReason

  • New Member
  • *
  • Posts: 21
Re: why skip read in for loop
« Reply #4 on: November 28, 2017, 03:39:24 pm »
When i Use Comparetext got "project1.lpr(37,6) Error: Identifier not found "CompareText"

But i found that i can write too like a>b (both strings) and this will give me true or not, so its practically same.

Handoko

  • Hero Member
  • *****
  • Posts: 5149
  • My goal: build my own game engine using Lazarus
Re: why skip read in for loop
« Reply #5 on: November 28, 2017, 03:44:43 pm »
If I'm not wrong, you need to put sysutils in your uses clause if you want to use CompareText.

https://www.freepascal.org/docs-html/rtl/sysutils/comparetext.html

 

TinyPortal © 2005-2018