Smartest way to read a number?

2011-11-10 Thread Fabian
Hey guys. I just want to write a few console applications. Usualy I have to read numbers to calculate some values. But what's the smartest way to read and convert the input? I've coded these lines: import std.stdio, std.string, std.conv; T readNumber(T)() { string buffer;

Re: Smartest way to read a number?

2011-11-10 Thread Tobias Brandt
import std.stdio; T readNumber(T)() { T result; stdin.readf(%s, result); return result; } Throws a ConvException if the input string wasn't in the right format. On 10 November 2011 22:48, Fabian talk2...@online.de wrote: Hey guys. I just want to write a few console applications.

Re: Smartest way to read a number?

2011-11-10 Thread Andrej Mitrovic
This: https://github.com/he-the-great/JPDLibs/tree/cmdln/cmdln The HTML docs explain how to use it. Not mine, but I think this would be a sweet addition to Phobos imo.

Re: Smartest way to read a number?

2011-11-10 Thread Fabian
Thanks a lot. That's exactly what I was searching for.

Re: Smartest way to read a number?

2011-11-10 Thread Fabian
Andrej Mitrovic wrote: This: https://github.com/he-the-great/JPDLibs/tree/cmdln/cmdln The HTML docs explain how to use it. Not mine, but I think this would be a sweet addition to Phobos imo. Thank you. :)

Re: Smartest way to read a number?

2011-11-10 Thread Jonathan M Davis
On Thursday, November 10, 2011 13:48 Fabian wrote: Hey guys. I just want to write a few console applications. Usualy I have to read numbers to calculate some values. But what's the smartest way to read and convert the input? I've coded these lines: import std.stdio, std.string,

Re: Smartest way to read a number?

2011-11-10 Thread Kai Meyer
I don't get the exception on Linux after a new line, I have to wait until EOF, which is typically the end of the program if reading from STDIN. Not very useful. import std.stdio; T readNumber(T)() { T result; stdin.readf(%s, result); return result; } void main() { try {

Re: Smartest way to read a number?

2011-11-10 Thread Tobias Brandt
Yes, you are right. You can make it work by changing to line buffering: stdin.setvbuf(null, _IOLBF); But at that point, another solution (like using std.conv.to) is probably the better choice. On 10 November 2011 23:40, Kai Meyer k...@unixlords.com wrote: I don't get the exception on Linux

Re: Smartest way to read a number?

2011-11-10 Thread Fabian
Ok - Good to know. Thank you ;)