www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Calling readln() after readf

reply PinDPlugga <a a.com> writes:
I didn't want to necropost, but I ran into the same behaviour as 
in this post:
https://forum.dlang.org/post/yfybveovbknvvxmioivd forum.dlang.org
and was just curious to understand it better.

If I call readln() after having previously called readf(), it 
does not behave as expected:
```d
import std.stdio, std.string;

void main()
{
     write("Please enter a number: ");
     double number;
     readf(" %s", number);

     write("Please enter a string: ");
     string input = strip(readln());

     writefln!("number: %s --- string: %s")(number, input);
}
```
Gives me the following:
```
Please enter a number: 1
Please enter a string: number: 1 ---- string:
```
I know I can get this to work replacing the `strin(readln())` 
with `readf(" %s\n", input)`, but I also found if I just call 
`strip(readln())` an extra time this worked as well, but what is 
confusing to me is if I have multiple readf's I still only need 
to call readln one extra time to get it to work as expected:
```d
import std.stdio, std.string;

void main()
{
     write("Please enter a number: ");
     double number1;
     readf(" %s", number1);
     write("Please enter a number: ");
     double number2;
     readf(" %s", number2);

     // Handle what should be two \n's from readf?
     string input = strip(readln());

     // Continue as normal
     write("Please enter a string: ");
     input = strip(readln());

     writefln!("number1: %s --- number2: %s --- string: %s")
         (number1, number2, input);
}
```
And this works.
```
Please enter a number: 1
Please enter a number: 2
Please enter a string: hello
number1: 1 --- number2: 2 --- string: hello
```
Could anyone help explain this to me, and also is there a better 
way to handle this when wanting to use a mix of readf and readln?
Apr 24
next sibling parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Saturday, 24 April 2021 at 14:46:06 UTC, PinDPlugga wrote:
 I didn't want to necropost, but I ran into the same behaviour 
 as in this post:
 https://forum.dlang.org/post/yfybveovbknvvxmioivd forum.dlang.org
 and was just curious to understand it better.

 [...]
https://dlang.org/library/std/string/chomp.html
Apr 24
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 4/24/21 7:46 AM, PinDPlugga wrote:

      write("Please enter a number: ");
      double number;
      readf(" %s", number);
Just to make sure, this is a common issue for other languages as well. As the explanation, the character ('\n') that you injected into stdin by pressing Enter is not (and should not be) removed by readf.
      write("Please enter a string: ");
      string input = strip(readln());
Because of that '\n' character, that readln() reads an empty line. There is no way other than reading and discarding all input at stdin but luckily, readf's "formatted" behavior is able to take care of it because any character that match the format string will be read and discarded by readf: readf(" %s\n", number); As a side note, that '\n' should work on all platforms even if the terminal injects two characters. As a general solution, you can use a function like this: auto readLine(S = string)(File file = stdin) { while (!file.eof) { auto line = file.readln!S.strip; if (!line.empty) { return line; } } return null; } Ali
Apr 24