Calling readln() after readf

Gary Chike chikega at gmail.com
Wed Jul 6 00:14:39 UTC 2022


On Monday, 20 June 2022 at 16:08:33 UTC, Ali Çehreli wrote:
> On 6/20/22 07:00, Gary Chike wrote:
>
> > 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
>
> Makes sense. The following are related as well:
>
>   https://dlang.org/library/std/conv/parse.html
>
>   https://dlang.org/library/std/format/read/formatted_read.html
>
> Ali

It's interesting from my own experimentation and observation, 
comparing `parse!int()` vs `to!int()` For instance, I can 
formulate a statement such as this using `to!int()`:
`auto age = to!int(readln.strip());`
but the same syntax  using `parse!int()` would generate an error:
`auto age = parse!int(readln.strip());`
So, in order to use `parse!int()`, I would need to separate it 
into two statements with a variable acting as an intermediary:
```
auto input = readln();
auto age = parse!int(input);



More information about the Digitalmars-d-learn mailing list