I tried this but it give me Runtime error 106.
a, c: char;
b, d: longint;
read(a, b, c, d);
write(a, ' ', b, ' ', c, ' ', d);
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:'); // Read entire line Readln(s); // Split s into four entries in the string list sl.Delimiter := ' '; sl.DelimitedText := s; if sl.Count >= 4 then begin a := sl; // first string item, convert to char b := StrToInt(sl); // second string item, convert to integer c := sl; // third string item, convert to char d := StrToInt(sl); // 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.