Read integer from console and sleep
Iain
staffell at gmail.com
Fri May 4 06:18:15 PDT 2012
I tried to use the sleep function from the Rosetta Code project:
http://rosettacode.org/wiki/Sleep#D
Am I right in thinking this is D1 code, as it will not compile
under 2.059?
So, I tried writing my own version, and hit my head against many
brick walls along the way. My result takes more lines than C,
C++ or C#, so I'm guessing it can't be the best solution.
Could anyone explain how to optimise the code below before I post
it to Rosetta?
-----------------
import std.stdio;
import std.conv;
import std.string;
import core.thread;
void main()
{
write("Enter a time to sleep (in seconds): ");
string input = stdin.readln(); // have to make this a string
first, it can't go directly into parse!
auto time = parse!long(input); // have to use parse!, as to!
crashes when it sees a \n
writeln("Sleeping...");
Thread.sleep( dur!("seconds")(time) );
writeln("Awake!");
}
Many thanks!
More information about the Digitalmars-d-learn
mailing list