Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-04

Read() for multiple variables.

2 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

zaher dirkey

I am old pascal programmer, but now i teaching pascal and need to understand the old input

program readtest;
var
  x:integer;
  y:integer;
  s:string;
begin
  read(x, y, s);
  writeln();
  writeln(x,y,'"',s,'"');
  readln;
end.
-------------
10 50 Hello

how to use read() procedure to read multiple variable one of them is string type
in example above it read x, y correclty, but variable s take a first space before the text.
How i can manage to read the string after integer with simple read?.

Zaher Dirkey

Reply by email

Unknown author
to zaher dirkey
zaher dirkey wrote
how to use read() procedure to read multiple variable one of them is string type

To read a string and then number it is possible only if the length of a line is precisely known and fixed.
I read out one string, and then I find in it numbers. Here an example for a finding of real:

procedure FindReal(Source: String; VAR Destign: Real; VAR NBeg, CodeOp: Integer); CONST BegOfNumb: SET OF CHAR = ['0'..'9']; NumbChars: SET OF CHAR = ['0'..'9','E','e','+','-','.']; VAR Buf_String: String; Buf_Real : Real; len : Integer; BEGIN CodeOp:=-30; len:=LENGTH(Source); if (NBeg<=0) then EXIT; if NBeg>=len then EXIT; Buf_String:=''; While (NOT (Source[NBeg] in BegOfNumb)) AND (NBeg < len) do INC(NBeg); if NBeg=len then EXIT; if NBeg>1 then if (Source[NBeg-1]='+') OR (Source[NBeg-1]='-') then DEC(NBeg,1); While (Source[NBeg] in NumbChars) AND (NBeg<=len) do begin Buf_String:=Buf_String + Source[NBeg]; INC(NBeg) end; Val(Buf_String, Buf_Real, CodeOp); if CodeOp=0 then Destign:=Buf_Real END; --- Boris

Reply by email