Following up from the reddit announcement
bearophile
bearophileHUGS at lycos.com
Mon Feb 24 14:34:38 PST 2014
> Also the code throws if just hit enter (it means you enter no
> number). So the code is still bad.
I prefer code like this:
void main() {
import std.stdio, std.algorithm, std.string, std.array,
std.conv;
int[] even, odd;
while (true) {
"Enter a number (or just enter to exit): ".write;
immutable txt = readln.strip;
if (txt.empty)
break;
try {
immutable input = txt.to!int;
if (input % 2 == 0) {
"even".writeln;
even ~= input;
} else {
"odd".writeln;
odd ~= input;
}
} catch (ConvException e) {
"Not a number.".writeln;
continue;
}
}
odd.sort().writeln;
even.sort().writeln;
}
More information about the Digitalmars-d
mailing list