Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 27, 2015 readln() doesn't stop to read the input. | ||||
---|---|---|---|---|
| ||||
Please, i need your help, I tried this: write("Write p: "); readln(p); p = chomp(p); writeln("Write q: "); readln(q); q = chomp(q); but the result is: Write p: Write q: and doesn't pause to read keyboard input... what's wrong? Thanks in advance! |
March 27, 2015 Re: readln() doesn't stop to read the input. | ||||
---|---|---|---|---|
| ||||
Posted in reply to jonaspm | On Friday, 27 March 2015 at 04:37:34 UTC, jonaspm wrote: > Please, i need your help, I tried this: > > write("Write p: "); > readln(p); > p = chomp(p); > writeln("Write q: "); > readln(q); > q = chomp(q); > > but the result is: > Write p: Write q: > > and doesn't pause to read keyboard input... what's wrong? > > Thanks in advance! http://dlang.org/phobos/std_stdio.html#.readln Check the example, and you will see the problem. readln(p) doesn't read what was typed into p. |
March 27, 2015 Re: readln() doesn't stop to read the input. | ||||
---|---|---|---|---|
| ||||
Posted in reply to tcak | but readln(p); isn't supposed to read input and store it into p? |
March 27, 2015 Re: readln() doesn't stop to read the input. | ||||
---|---|---|---|---|
| ||||
Posted in reply to jonaspm | On Friday, 27 March 2015 at 04:37:34 UTC, jonaspm wrote:
> Please, i need your help, I tried this:
>
> write("Write p: ");
> readln(p);
> p = chomp(p);
> writeln("Write q: ");
> readln(q);
> q = chomp(q);
>
> but the result is:
> Write p: Write q:
>
> and doesn't pause to read keyboard input... what's wrong?
>
> Thanks in advance!
This works for me on Linux:
---
import std.stdio;
import std.string;
void main()
{
char[] p, q;
p.length=1024;
q.length=1024;
write("Write p: ");
readln(p);
p=p.chomp;
write("Write q: ");
readln(q);
q=q.chomp;
writeln; writefln("p=%s, q=%s", p,q);
}
---
bye,
lobo
|
March 27, 2015 Re: readln() doesn't stop to read the input. | ||||
---|---|---|---|---|
| ||||
Posted in reply to jonaspm | On Friday, 27 March 2015 at 04:37:34 UTC, jonaspm wrote: > Please, i need your help, I tried this: > > write("Write p: "); > readln(p); > p = chomp(p); > writeln("Write q: "); > readln(q); > q = chomp(q); > > but the result is: > Write p: Write q: > > and doesn't pause to read keyboard input... what's wrong? > > Thanks in advance! The easiest to use is the form "<string> = readln()": write("Write p: "); auto p = readln().chomp(); write("Write q: "); auto q = readln().chomp(); The second version, "<length> = readln(<buffer>)", goes something like: auto buf = new char[1024]; write("Write p: "); string p = buf[0..readln(buf)].chomp().idup; write("Write q: "); string q = buf[0..readln(buf)].chomp().idup; It allows to reuse a preallocated buffer for greater speed and finer allocation control. Also worth noting, the best place for such questions in D.learn group: http://forum.dlang.org/group/digitalmars.D.learn Ivan Kazmenko. |
March 27, 2015 Re: readln() doesn't stop to read the input. | ||||
---|---|---|---|---|
| ||||
Posted in reply to jonaspm | 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();
|
March 27, 2015 Re: readln() doesn't stop to read the input. | ||||
---|---|---|---|---|
| ||||
Posted in reply to tcak | 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
|
March 28, 2015 Re: readln() doesn't stop to read the input. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | module main; import std.stdio; import std.string; int main(string[] args) { int resp; char[] p, q; writefln("MENU DE OPCIONES"); writefln("1) Modus Ponens"); writefln("2) Modus Tollens"); writefln("3) Silogismo Hipotetico"); writefln("4) Salir"); do{ writeln("Introduce la opcion que deseas: "); readf(" %d", &resp); }while(resp<1 || resp>4); write("Write p:"); readln(p); // This is skipped. ¿? p = chomp(p); write("Write q:"); readln(q); q = chomp(q); writeln("p: ",p); // Doesn't write anything. writeln("q: ",q); // Writes what you typed in the keyboard back on readln(); return 0; } This is the entire code. Environment: Windows 7 Home Premium x64 IDE: CodeBlocks 13.12 Compiler: Digital Mars D Compiler |
March 28, 2015 Re: readln() doesn't stop to read the input. | ||||
---|---|---|---|---|
| ||||
Posted in reply to jonaspm | On Saturday, 28 March 2015 at 03:07:31 UTC, jonaspm wrote:
> module main;
>
> import std.stdio;
> import std.string;
>
> int main(string[] args)
> {
> int resp;
> char[] p, q;
>
> writefln("MENU DE OPCIONES");
> writefln("1) Modus Ponens");
> writefln("2) Modus Tollens");
> writefln("3) Silogismo Hipotetico");
> writefln("4) Salir");
>
> do{
> writeln("Introduce la opcion que deseas: ");
> readf(" %d", &resp);
> }while(resp<1 || resp>4);
>
> write("Write p:");
> readln(p); // This is skipped. ¿?
> p = chomp(p);
> write("Write q:");
> readln(q);
> q = chomp(q);
>
>
> writeln("p: ",p); // Doesn't write anything.
>
> writeln("q: ",q); // Writes what you typed in the keyboard
> back on readln();
>
>
> return 0;
> }
The problem is that the `readf` call doesn't read the first line completely. It leaves the newline. The first `readln` call then picks up that newline and stops right there, resulting in an empty line.
I'm not sure how the `readf` could be changed to consume the newline. I tried " %d\n" and " %d ". They don't seem to do it.
But you can add a `readln();` after the `readf` call and it works.
|
Copyright © 1999-2021 by the D Language Foundation