How to use "read_bool"?

Zeh zecacu at yahoo.com.br
Mon Aug 6 09:17:32 PDT 2012


On Saturday, 4 August 2012 at 08:35:36 UTC, simendsjo wrote:
> On Sat, 04 Aug 2012 03:01:31 +0200, Zeh <zecacu at yahoo.com.br> 
> wrote:
>
>> Thanks for the help, but i tryed both solutions posted and 
>> still not working. :/
>>
>> I get erros to compile the code posted by simendsjo. I try 
>> modify at my own, but without success. The code suggest by 
>> Timon Gehr compiles, but not work.
>>
>> Honestly speaking, i didn't have enough knowledge to 
>> understand the solutions offered (yet). What is the smallest 
>> way to use the "read_bool"?
>>
>>
>> Besides, one version of that tutorial in portuguese would be 
>> great =D
>
>
> This works:
> import std.stdio, std.string, std.conv;
>
> bool read_bool(in string message) {
>     while(true) {
>         write(message, " (false or true): ");
>         string input;
>         do {
>             input = readln().chomp();
>         } while(!input.length);
>         writeln("INPUT: '", input, "'");
>
>         try
>             return input.to!bool();
>         catch {}
>     }
> }
>
> void main() {
>     write("How many are we? ");
>     int personCount;
>     readf(" %s", &personCount);
>
>     write("How many bicycles are there? ");
>     int bicycleCount;
>     readf(" %s", &bicycleCount);
>
>     write("What is the distance to the beach? ");
>     int distance;
>     readf(" %s", &distance);
>
>     bool existsCar = read_bool("Is there a car? ");
>     bool existsLicense =
>         read_bool("Is there a driver license? ");
> }
>
>
> $ rdmd read_bool
> How many are we? 10
> How many bicycles are there? 1
> What is the distance to the beach? 2
> Is there a car?  (false or true): true
> Is there a driver license?  (false or true): false

Very interesting way to solve the problem (at least for a noobie
like me ^^ ). Thanks for the help!



More information about the Digitalmars-d-learn mailing list