How to use Readln?

Write here if you have problems with your Pascal source code

Moderator: Board moderators

Post Reply
ggggqqqqihc
New poster
Posts: 12
Joined: Sun Dec 07, 2003 10:45 am

How to use Readln?

Post by ggggqqqqihc » Mon Dec 08, 2003 11:53 am

Look at the following program:
[pascal]var
a:integer;
c:char;
begin
readln(a,c);
writeln(a,c);
end.[/pascal]
I type '12 a'(not including" ' ") while the program is running. But c<>'a'. Why is it? How to solve it? I don't want to change the way of input.
Thank you.

junjieliang
Experienced poster
Posts: 169
Joined: Wed Oct 31, 2001 2:00 am
Location: Singapore

Post by junjieliang » Mon Dec 08, 2003 12:32 pm

I suppose c is the space character in this case. However, I don't really know how to solve this. :oops: The best way I can think of would be to have another (useless) variable that just reads away the space character, like this:
[pascal]
var
a : integer;
c, useless : char;
begin
readln(a, useless, c);
writeln(a, c);
end.
[/pascal]

And if you are not sure how many spaces will be between the integer and char, you can keep on reading until you get a character:
[pascal]
var
a : integer;
c : char;
begin
read(a);
repeat
read(c);
until (c in ['A'..'Z', 'a'..'z']);
readln;
end.
[/pascal]

Hope this helps.

Post Reply

Return to “Pascal”