www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Conversion error.

reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
I don't know any explanation for the following error:
std.conv.ConvException D:\Programs\D\dmd2\windows\bin\..\..\src\phob
s\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");
   }
}
Jan 27 2021
next sibling parent Ruby The Roobster <michaeleverestc79 gmail.com> writes:
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\phob
s\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.
Jan 27 2021
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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.
Jan 27 2021
parent Ruby The Roobster <michaeleverestc79 gmail.com> writes:
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!
Jan 27 2021