Absolute beginner

Timon Gehr timon.gehr at gmx.ch
Fri Jan 13 13:40:07 PST 2012


On 01/13/2012 10:34 PM, Jorge wrote:
> Thanks for your answer but:
>
> import std.stdio;
> import std.conv;
> void main()
> {
> 	write("Insert number: ");
> 	string s = readln();
> 	auto i = to!int(s);
> }
>
> compiles but after i enter a number and press the enter key i get:
>
> std.conv.ConvException at .\..\..\src\phobos\std\conv.d(1595): Can't
> convert value
> `
> ' of type string to type int
> ----------------
> 41DECC
> 41DD43
> 4027F3
> 402475
> 402D6C
> 402DB0
> 4029A7
> 4BBA79
> ----------------
>
> what's wrong?

readln() includes the trailing newline character in the resulting 
string. You can use std.string.strip to remove leading and trailing 
whitespace:

import std.stdio;
import std.conv;
import std.string;
void main()
{
     write("Insert number: ");
     string s = readln();
     auto i = to!int(strip(s));
}


More information about the Digitalmars-d-learn mailing list