View previous topic :: View next topic |
Author |
Message |
AutoPython
Joined: 05 Dec 2009 Posts: 12
|
Posted: Mon Dec 14, 2009 7:06 pm Post subject: Getting an integer input/converting types |
|
|
I am a total newbie, and I have no idea how to get an integer input. Or if I can just use 'readln()' and convert the char[] type to the desired type. |
|
Back to top |
|
|
csauls
Joined: 27 Mar 2004 Posts: 278
|
Posted: Tue Dec 15, 2009 12:38 am Post subject: |
|
|
Something like this:
Code: | import std .conv ;
// ...
value = to!int( text ); |
to is your friend. ^_^ Depending on your usage, you might also want to look at parse, also from std.conv. Documentation:
http://digitalmars.com/d/2.0/phobos/std_conv.html _________________ Chris Nicholson-Sauls |
|
Back to top |
|
|
AutoPython
Joined: 05 Dec 2009 Posts: 12
|
Posted: Tue Dec 15, 2009 9:11 pm Post subject: |
|
|
Gives me this error :
Code: |
writef("Please type a number: ");
char[] input = readln();
input = to!int(input);
[color=red]
found '!' when expecting ';' following 'statement'
[/color]
|
[/code] |
|
Back to top |
|
|
michaelp
Joined: 27 Jul 2008 Posts: 114
|
Posted: Thu Dec 17, 2009 2:54 pm Post subject: |
|
|
I guess you are using D1.
Use toInt( input ) instead. You still import std.conv. |
|
Back to top |
|
|
csauls
Joined: 27 Mar 2004 Posts: 278
|
Posted: Fri Dec 18, 2009 3:31 am Post subject: |
|
|
I should really start making a point of asking that first... and of not assuming D2 when I answer... Oh well, sorry. _________________ Chris Nicholson-Sauls |
|
Back to top |
|
|
|