Yusufmm - 3 months ago 48
Pascal Question

# How to get input of multiple char and longint separated by a space in single read()?

I tried this but it give me Runtime error 106.

``````var
a, c: char;
b, d: longint;
begin
write(a, ' ', b, ' ', c, ' ', d);
end.
``````

The input is

``````A 1 B 2
``````

As @DavidHeffernan commented, and as @lurker explained, your code won't work as-is.

Assuming you have a FreePascal version that knows the `TStringList` type in unit `Classes` (I guess all recent ones do), you can do the following:

``````uses
Classes, SysUtils;

procedure Test;
var
a, c: Char;
b, d: Longint;
s: string;
sl: TStringList;
begin
sl := TStringList.Create;
try
Writeln('Enter a char, a number, a char and a number again, separated by spaces:');

// Split s into four entries in the string list
sl.Delimiter := ' ';
sl.DelimitedText := s;
if sl.Count >= 4 then
begin
a := sl[0][1];         // first string item, convert to char
b := StrToInt(sl[1]);  // second string item, convert to integer
c := sl[2][1];         // third string item, convert to char
d := StrToInt(sl[3]);  // fourth string item, convert to integer
end;
finally
sl.Free;
end;
Writeln(a, ' ', b, ' ', c, ' ', d);
end;

begin
Test;
end.
``````

You should probably add error checking for the case you get bad input, and perhaps use `TryStrToInt` instead of `StrToInt`, but I'll leave that as an exercise.

As @lurker said, in your question, you are trying to read Chars, which causes problems, because for instance a space and a tab are also a Chars, so the space delimiters don't work as you expected here. This will mess up the entire input. The only way to get these items is to read the entire string and then to parse it (split it).

If your version of FreePascal has a unit `StrUtils` with a `SplitString` function in it, you can use that instead of a `TStringList`. It splits the string, using a given delimiter (here `' '`) into a dynamic array. That is perhaps a little simpler, but not much.