readln() doesn't stop to read the input.
anonymous via Digitalmars-d
digitalmars-d at puremagic.com
Sat Mar 28 04:15:29 PDT 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.
More information about the Digitalmars-d
mailing list