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.
How to use Readln?
Moderator: Board moderators
-
- Experienced poster
- Posts: 169
- Joined: Wed Oct 31, 2001 2:00 am
- Location: Singapore
I suppose c is the space character in this case. However, I don't really know how to solve this.
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.
![:oops:](./images/smilies/icon_redface.gif)
[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.