Search code examples
console-applicationpascalfreepascal

why Read procedure insert New Line if user press the Enter keyboard?


i'm trying to work with "Read" procedure instead of "ReadLn" inside Free Pascal IDE but when i press enter after typing the value for it i get a new line like "readln" does....

program test;
uses
  crt;
var
  AName: string[20];

function Complete_Rectangle(ALength_NewValue, ALength_Rectangle, ALength_Pref: byte): string;
const
  Stars : string = '**';
var
  Space_Str: string;
  I,
  Space_Needed : byte;
begin
  Space_Needed := ALength_Rectangle - ALength_Pref; (* Calculate Needed Space.... *)
  Space_Str  := '';

  for I:=1 to Space_Needed -(ALength_NewValue + 2) do  (* 2 represent the length of Stars Constant *)
  begin
    Space_Str := Space_Str + ' ';
  end;

  Complete_Rectangle := Space_Str + Stars;
end;

begin
  clrscr;
  writeln('**********************************************************************');
  writeln('**                                                                  **');
    write('**  [*] add your Name here: '); read(AName); writeln(Complete_Rectangle(length(AName), 70, 28));
    (* 70 represent the length of rectangle spaces and 28 represent the length of this prefix text "**  [*] add your Name here: " *)
  writeln('**                                                                  **');
  writeln('**********************************************************************');
  readkey;
end.

is it possible to create my own custom Read to fix the problem above ? i have this code here but it seems doesn't work properly ...:

procedure Lire(AVariant: Variant);
var
  I: byte;
  AKey: Char;
  AReadStr: String;
begin
  I := 1;
  AReadStr := '';
  AKey := char('');

  repeat
    AKey := ReadKey;
    if not (AKey = #13) then
    begin
      AReadStr[I] := AKey;
      write(AKey);
      inc(I);
    end;
  Until AKey = #13;
  case varType(AVariant) of
    varString: begin AVariant := AReadStr; end;
    varInteger: begin AVariant := StrToInt(AReadStr); end;
    varByte: begin AVariant := StrToInt(AReadStr); end;
    varBoolean: begin AVariant := StrToBool(AReadStr); end;
    (* ..... and so on *)
  end;


end;

Solution

  • The Read and Readln statements are common for both, getting input from the keyboard AND reading data from a file.

    About Readln from the docs:

    reads one or more values from a file F, and stores the result in V1, V2, etc. After that it goes to the next line in the file.

    About Read from the docs:

    reads one or more values from a file F, and stores the result in V1, V2, etc.

    So when reading from a file, readln goes to the next line, while read does not. But this is not the expected behaviour with regards to console input.

    If you really want to position the cursor on the screen, I'd advise using the old gotXY command which is also available in FreePascal. Link to gotoXY

    eg:

    write('**  [*] add your Name here: '); 
    read(AName); 
    gotoXY(28 + length(AName)+ 1, WhereY-1); // Repositionate the cursor to the right Position.
    writeln(Complete_Rectangle(length(AName), 70, 28));