readln() doesn't stop to read the input.
Steven Schveighoffer via Digitalmars-d
digitalmars-d at puremagic.com
Fri Mar 27 04:06:59 PDT 2015
On 3/27/15 4:22 AM, tcak wrote:
> On Friday, 27 March 2015 at 05:17:03 UTC, jonaspm wrote:
>> but readln(p); isn't supposed to read input and store it into p?
>
> Nope. Parameter is terminator character. Read string is returned from
> the function. So, it would be like:
>
> string p = readln();
readln has an overload that looks like this:
size_t readln(C)(ref C[] buf, dchar terminator = '\x0a') if
(isSomeChar!C && is(Unqual!C == C) && !is(C == enum));
Which is what the OP is using (possibly, he never defines p and q).
As for the error, assuming you have proper buffer types for p and q, it
appears that your stdin is closed somehow. I think this is likely an
environmental issue. Please post the full code and describe the
environment if you need more help.
Your code works fine for me on OSX:
import std.stdio;
import std.string;
void main()
{
char[] p;
char[] q;
write("Write p: ");
readln(p);
p = chomp(p);
writeln("Write q: ");
readln(q);
q = chomp(q);
writeln(p, " ", q);
}
RUN:
Write p: adb;lna;lhiser
Write q:
slisieleru
adb;lna;lhiser slisieleru
-Steve
More information about the Digitalmars-d
mailing list