Thread overview
Error "Unexpected '\n' when converting from type LockingTextReader to type int"
Sep 07, 2022
Synopsis
Sep 07, 2022
rikki cattermole
Sep 07, 2022
Synopsis
Sep 07, 2022
rikki cattermole
Sep 08, 2022
Ali Çehreli
September 07, 2022

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):

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:

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\phobos\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! :)

September 08, 2022
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
September 07, 2022

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.

September 08, 2022
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.

September 07, 2022
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