www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - readln() doesn't stop to read the input.

reply "jonaspm" <jonaspm_99 outlook.com> writes:
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!
Mar 26 2015
next sibling parent reply "tcak" <tcak gmail.com> writes:
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!
Check the example, and you will see the problem. readln(p) doesn't read what was typed into p.
Mar 26 2015
parent reply "jonaspm" <jonaspm_99 outlook.com> writes:
but readln(p); isn't supposed to read input and store it into p?
Mar 26 2015
parent reply "tcak" <tcak gmail.com> writes:
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();
Mar 27 2015
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Mar 27 2015
parent reply "jonaspm" <jonaspm_99 outlook.com> writes:
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
Mar 27 2015
parent "anonymous" <anonymous example.com> writes:
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.
Mar 28 2015
prev sibling next sibling parent "lobo" <swamplobo gmail.com> writes:
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
Mar 26 2015
prev sibling next sibling parent "Ivan Kazmenko" <gassa mail.ru> writes:
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.
Mar 27 2015
prev sibling parent reply "Jacques =?UTF-8?B?TcO8bGxlciI=?= <jacques.mueller gmx.de> writes:
You can clear the input stream:

 while(getchar() != '\n') {};
Or just use readln:
 int resp = readln.chomp.to!int;
Mar 28 2015
parent "jonaspm" <jonaspm_99 outlook.com> writes:
I see... thanks to everyone for helping me out!
Mar 30 2015