Thread overview
Conversion error.
Jan 28, 2021
Ruby The Roobster
Jan 28, 2021
Ruby The Roobster
Jan 28, 2021
Adam D. Ruppe
Jan 28, 2021
Ruby The Roobster
January 28, 2021
I don't know any explanation for the following error:
std.conv.ConvException@D:\Programs\D\dmd2\windows\bin\..\..\src\phobos\std\conv.d(2437): Unexpected '\n' when converting from type LockingTextReader to type int

Here is my code for reference:

module main;

import std.stdio;
import std.concurrency;
import core.thread;
import core.stdc.stdlib;
import std.string;
int main(string[] args)
{
    Tid worker;
    int x;
    do
    {
    writeln("Enter in 1 for arabic, 2 for turkish, 0 to quit.");
    readf("%d",x);
    worker = spawn(&Translate_Hello);
    worker.send(x);
    }
    while(x != 0);
	return 0;
}

void Translate_Hello()
{
    int num;
    num = receiveOnly!int();
 switch(num)
  {
    case 1:
        stdout.writeln("مرحبا");
        break;
    case 2:
        stdout.writeln("Merhaba");
        break;
    default:
        stdout.writeln("Invalid Language");
  }
}

January 28, 2021
On Thursday, 28 January 2021 at 01:01:36 UTC, Ruby The Roobster wrote:
> I don't know any explanation for the following error:
> std.conv.ConvException@D:\Programs\D\dmd2\windows\bin\..\..\src\phobos\std\conv.d(2437): Unexpected '\n' when converting from type LockingTextReader to type int

In addition, this only gives that exception when you enter 1 or 2, so I don't know what is happening.
January 28, 2021
On Thursday, 28 January 2021 at 01:01:36 UTC, Ruby The Roobster wrote:
>     readf("%d",x);

This is why I hate readf, it is sensitive to litte things.

If you put a space in that string I think it will fix it. What happens here is it reads the float, then leaves the buffer at the \n from when the user pressed enter. So when it comes around it complains about that unexpected character.

So try readf(" %d") or readf("%d\n") or something like that.

OR just use `readln().strip().to!int` that kind fo thing.
January 28, 2021
On Thursday, 28 January 2021 at 01:09:52 UTC, Adam D. Ruppe wrote:
> On Thursday, 28 January 2021 at 01:01:36 UTC, Ruby The Roobster wrote:
>>     readf("%d",x);
>
> This is why I hate readf, it is sensitive to litte things.
>
> If you put a space in that string I think it will fix it. What happens here is it reads the float, then leaves the buffer at the \n from when the user pressed enter. So when it comes around it complains about that unexpected character.
>
> So try readf(" %d") or readf("%d\n") or something like that.
>
> OR just use `readln().strip().to!int` that kind fo thing.

Thanks!