Jump to page: 1 2
Thread overview
readln() doesn't stop to read the input.
Mar 27, 2015
jonaspm
Mar 27, 2015
tcak
Mar 27, 2015
jonaspm
Mar 27, 2015
tcak
Mar 28, 2015
jonaspm
Mar 28, 2015
anonymous
Mar 27, 2015
lobo
Mar 27, 2015
Ivan Kazmenko
Mar 28, 2015
Jacques Müller
Mar 30, 2015
jonaspm
March 27, 2015
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
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
but readln(p); isn't supposed to read input and store it into p?
March 27, 2015
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
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
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
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
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
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.
March 28, 2015
You can clear the input stream:

> while(getchar() != '\n') {};

Or just use readln:

> int resp = readln.chomp.to!int;

« First   ‹ Prev
1 2