www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why does this not work?

reply TheDGuy <loder.feuer googlemail.com> writes:
     writeln("Which number should i guess?");
	string input = readln();
	int i = to!int(input);
Jan 01
next sibling parent reply Tobi G. <gruen_tobias web.de> writes:
On Friday, 1 January 2016 at 14:00:41 UTC, TheDGuy wrote:
     writeln("Which number should i guess?");
 	string input = readln();
 	int i = to!int(input);
The solution is that readln() returns a string that also contains the newline this can be solved by easily stripping the newline off import std.string; int i = to!int(input.strip);
Jan 01
parent reply Tobi G. <gruen_tobias web.de> writes:
On Friday, 1 January 2016 at 14:20:26 UTC, Tobi G. wrote:
 The solution is that readln() returns a string that also 
 contains the newline
 this can be solved by easily stripping the newline off

 import std.string;

 int i = to!int(input.strip);
Sorry my bad english.. i wrote solution but meant problem
Jan 01
parent reply TheDGuy <loder.feuer googlemail.com> writes:
On Friday, 1 January 2016 at 14:29:34 UTC, Tobi G. wrote:
 On Friday, 1 January 2016 at 14:20:26 UTC, Tobi G. wrote:
 The solution is that readln() returns a string that also 
 contains the newline
 this can be solved by easily stripping the newline off

 import std.string;

 int i = to!int(input.strip);
Sorry my bad english.. i wrote solution but meant problem
Thank you very much! That helped me alot. It is kind of hard if you don't have the background knowledge...
Jan 01
parent reply bachmeier <no spam.net> writes:
On Friday, 1 January 2016 at 14:47:20 UTC, TheDGuy wrote:
 On Friday, 1 January 2016 at 14:29:34 UTC, Tobi G. wrote:
 On Friday, 1 January 2016 at 14:20:26 UTC, Tobi G. wrote:
 The solution is that readln() returns a string that also 
 contains the newline
 this can be solved by easily stripping the newline off

 import std.string;

 int i = to!int(input.strip);
Sorry my bad english.. i wrote solution but meant problem
Thank you very much! That helped me alot. It is kind of hard if you don't have the background knowledge...
I've battled with a few times, not having any idea what was going on. I now almost automatically use strip when it's not working.
Jan 01
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Friday, 1 January 2016 at 15:06:53 UTC, bachmeier wrote:
 I've battled with a few times, not having any idea what was 
 going on. I now almost automatically use strip when it's not 
 working.
This is one of the most frequently asked questions by new users.. I added a tip to my new docs: http://dpldocs.info/experimental-docs/std.stdio.readln.html If you were looking at that for the first time, would you notice it? If no, I can see about moving it or rewording it or something.
Jan 01
next sibling parent reply TheDGuy <loder.feuer googlemail.com> writes:
On Friday, 1 January 2016 at 15:16:36 UTC, Adam D. Ruppe wrote:
 On Friday, 1 January 2016 at 15:06:53 UTC, bachmeier wrote:
 I've battled with a few times, not having any idea what was 
 going on. I now almost automatically use strip when it's not 
 working.
This is one of the most frequently asked questions by new users.. I added a tip to my new docs: http://dpldocs.info/experimental-docs/std.stdio.readln.html If you were looking at that for the first time, would you notice it? If no, I can see about moving it or rewording it or something.
If i had known that blog existed. I think your example at the end of the page explains the problem really well. Thanks!
Jan 01
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Friday, 1 January 2016 at 17:00:23 UTC, TheDGuy wrote:
 If i had known that blog existed. I think your example at the 
 end of the page explains the problem really well.
 Thanks!
I just wrote that example in response to this thread :P (the overall thing there is something i started last week to reorganize the official docs to be easier to read and edit) But it is a common enough question that it should just be in the documentation.
Jan 01
prev sibling parent bachmeier <no spam.net> writes:
On Friday, 1 January 2016 at 15:16:36 UTC, Adam D. Ruppe wrote:
 On Friday, 1 January 2016 at 15:06:53 UTC, bachmeier wrote:
 I've battled with a few times, not having any idea what was 
 going on. I now almost automatically use strip when it's not 
 working.
This is one of the most frequently asked questions by new users.. I added a tip to my new docs: http://dpldocs.info/experimental-docs/std.stdio.readln.html If you were looking at that for the first time, would you notice it? If no, I can see about moving it or rewording it or something.
If the user is reading the docs completely, yes, it is clear. It would probably be better to put it in its own section, because a user might not expect to find an explanation of a pitfall in there, and because it makes it easy to find when you're looking the second or third time.
Jan 01
prev sibling parent israel <tl12000 live.com> writes:
On Friday, 1 January 2016 at 14:00:41 UTC, TheDGuy wrote:
     writeln("Which number should i guess?");
 	string input = readln();
 	int i = to!int(input);
You fell for the C# syntax like me... According to Ahli. You have to use the old C way of doing it with readf.
Jan 01