Calling readln() after readf
Gary Chike
chikega at gmail.com
Mon Jun 20 14:00:08 UTC 2022
On Monday, 20 June 2022 at 00:43:17 UTC, Ali Çehreli wrote:
> ...
> But when the program interacts with piped data, there may be
> multiple \n characters and all of those might have to be
> ignored before reading the next non-empty line. So you may want
> to do readln.strip multiple times? I don't know. It all depends
> on the use case.
>
> Ali
Thank you for your input, Ali! Would it be appropriate to forego
`readf` and read input as a string using `readln` ,benefiting
from the `strip` function, then convert to their appropriate
datatype as in the following example?:
```d
module hello_world;
import std.stdio;
import std.string; // strip()
import std.conv; // to!int(),to!float()
void main() {
writeln("Hello world from D!");
write("What is your name? ");
auto fname = readln.strip;
writeln("It's so nice to meet you ", fname, "!");
write("How old are you? ");
auto age = to!int(readln.strip);
write("What is your GPA? ");
auto gpa = to!float(readln.strip);
writeln("So, ", fname, ", you are ", age, " years old and your
GPA is ",
gpa, ".. stellar!");
}
```
Also I signed up with Educative.io to do your interactive D
language lessons. It really goes so well with your book :)
Cheers,
Gary Chike
More information about the Digitalmars-d-learn
mailing list