Input handling? (newbie alert!)

Bernard Helyer b.helyer at gmail.com
Thu Sep 9 17:48:47 PDT 2010


I've not time for a more full answer now (I'll try later), but please, 
for the love of God, don't use scanf! As a general hint, use 
std.stdio.readln to get input as a string, then use the `to` function 
found in std.conv to convert it into what you want:

   auto input = readln();
   auto asInteger = to!int(input);

To handle errors, you'll probably want to catch whatever it is that to 
throws on failure:

   int i;
   auto input = readln();
   try {
       i = to!int(input);
   } catch (TheThingThatToThrows) {
       i = 0;
   }

I don't know how to!int handles entry of floating point numbers, if it 
doesn't, what you may want to do:

    int i;
    auto input = readln();
    try {
        i = cast(int) to!double(input);
    } catch (TheThingThatToThrows) {
        i = 0;
    }

---

Sorry I couldn't be more thorough. I hope that helps!


More information about the Digitalmars-d-learn mailing list