FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Getting an integer input/converting types

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
AutoPython



Joined: 05 Dec 2009
Posts: 12

PostPosted: Mon Dec 14, 2009 7:06 pm    Post subject: Getting an integer input/converting types Reply with quote

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
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Tue Dec 15, 2009 12:38 am    Post subject: Reply with quote

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
View user's profile Send private message AIM Address Yahoo Messenger
AutoPython



Joined: 05 Dec 2009
Posts: 12

PostPosted: Tue Dec 15, 2009 9:11 pm    Post subject: Reply with quote

Gives me this error Exclamation :

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
View user's profile Send private message
michaelp



Joined: 27 Jul 2008
Posts: 114

PostPosted: Thu Dec 17, 2009 2:54 pm    Post subject: Reply with quote

I guess you are using D1.
Use toInt( input ) instead. You still import std.conv.
Back to top
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Fri Dec 18, 2009 3:31 am    Post subject: Reply with quote

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
View user's profile Send private message AIM Address Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group