Search code examples
pascallazarus

Separating numbers in a string. Pascal


I have a problem. I'm learning Pascal for only a couple of weeks and I don't know much. I have to write a program that has to calculate something out of 3 entered numbers. The problem is all 3 of them need to be entered in one edit with spaces in between. So basically I have a string 'number number number'. How do I separate these numbers as 3 separate strings so I can convert them into Integer.


Solution

  • In pascal there are built-in procedures to retrieve the input from the console.

    The easiest way to get numeric inputs is to use Read()/ReadLn(), which also can make the conversion from string to a numeric value:

    procedure GetNumbers(var x,y,z: Integer); 
    begin 
      WriteLn('Enter three numbers separated with space and then press enter.');
      ReadLn(x,y,z); 
    end;
    

    Here, the ReadLn() detects three inputs separated with a space, waits for the [Enter] key and assigns the integer values to the x,y,z variables.