www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Error "Unexpected '\n' when converting from type LockingTextReader to

reply Synopsis <a a.com> writes:
Hi! I'm new at programming, just discovered D and I'm loving it 
so far!

I'm learning the basics, so please forgive my noob mistakes and 
questions (as well as my Engrish...).

I'm trying to make this program print both numbers entered by the 
user, it works with the first number (num):

```d
import std.stdio;

void main(){
   struct Fraction
   {
     int num;
     int den;
   }

   Fraction f1;

   write("Num: ");
   readf("%s", &f1.num);

   writef("Num is: %s", f1.num);
}
```


But when I try the same with the second one, I get an error after 
entering the first number:

```d
import std.stdio;

void main(){
   struct Fraction
   {
     int num;
     int den;
   }

   Fraction f1;

   write("Num: ");
   readf("%s", &f1.num);

   write("Den: ");
   readf("%s", &f1.den);

   writef("Num is: %s", f1.num);
   writef("Den is: %s", f1.den);
}
```


*std.conv.ConvException C:\D\dmd2\windows\bin\..\..\src\phob
s\std\conv.d(2526): Unexpected '\n' when converting from type LockingTextReader
to type int
0x00C80D74
0x00C80A77
0x00C80559
0x00C8047D
0x00C7FEDB
0x00C7FE4F
0x00C71045
0x00C8CE23
0x00C8CD8F
0x00C8CC0C
0x00C81D6C
0x00C7107F
0x75ADFA29 in BaseThreadInitThunk
0x77957A9E in RtlGetAppContainerNamedObjectPath
0x77957A6E in RtlGetAppContainerNamedObjectPath*


May someone tell me what am I doing wrong? Thank you in advance! 
:)
Sep 07 2022
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
Text in buffer: "123\n"

Read: "123"
Text in buffer: "\n"

Read: exception, expecting number for "\n"

Changing your readf format specifier to include the new line should work.

https://dlang.org/phobos/std_stdio.html#.File.readf
Sep 07 2022
parent reply Synopsis <a a.com> writes:
On Wednesday, 7 September 2022 at 23:06:44 UTC, rikki cattermole 
wrote:
 Text in buffer: "123\n"

 Read: "123"
 Text in buffer: "\n"

 Read: exception, expecting number for "\n"

 Changing your readf format specifier to include the new line 
 should work.

 https://dlang.org/phobos/std_stdio.html#.File.readf
Thank you! Adding the \n seems to solve my problem: ```readf("%s\n", &f1.num)``` I have two further questions about this! a- What is the difference with this syntax with the exclamation mark? ```readf!"%s\n"(f1.num);``` b- Do I need to put ```/n``` in every readf statement? I mean, considering that every input gets entered after pressing intro key (and I guess this is what introduce the \n) Forgive me if I'm asking silly questions.
Sep 07 2022
next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
On 08/09/2022 11:24 AM, Synopsis wrote:
 On Wednesday, 7 September 2022 at 23:06:44 UTC, rikki cattermole wrote:
 Text in buffer: "123\n"

 Read: "123"
 Text in buffer: "\n"

 Read: exception, expecting number for "\n"

 Changing your readf format specifier to include the new line should work.

 https://dlang.org/phobos/std_stdio.html#.File.readf
Thank you! Adding the \n seems to solve my problem: ```readf("%s\n", &f1.num)``` I have two further questions about this! a- What is the difference with this syntax with the exclamation mark? ```readf!"%s\n"(f1.num);```
That is a template. It'll type check that the format specifier matches your arguments. https://tour.dlang.org/tour/en/basics/templates
 b- Do I need to put ```/n``` in every readf statement? I mean, 
 considering that every input gets entered after pressing intro key (and 
 I guess this is what introduce the \n)
Yes.
Sep 07 2022
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 9/7/22 16:24, Synopsis wrote:

 a- What is the difference with this syntax with the exclamation mark?
 ```readf!"%s\n"(f1.num);```
That's the templated version, which is safer because it checks at compile time (important distinction) that the arguments and the format specifiers do match.
 b- Do I need to put ```/n``` in every readf statement?
Another option is to use a space character, which reads and skips any number of any whitespace character. I have written something about that here: http://ddili.org/ders/d.en/input.html And this one talks about readln, which may be more suitable in some cases: http://ddili.org/ders/d.en/strings.html And there is formattedRead: http://ddili.org/ders/d.en/strings.html#ix_strings.formattedRead
 Forgive me if I'm asking silly questions.
There is never a silly question. If a question came up, it is as legitimate as it gets. And welcome to programming! :) Ali
Sep 07 2022