[your code here]

MattCodr mattcoder at hotmail.com
Fri Feb 17 17:07:51 PST 2012


Nice, but just a little thing:

switch(toUpper(returned)) {
       case 'Y': break checkLoop;
       case 'H': {low=guess; break;}
       case 'L': {high=guess; break;}
       default: break;
}

PS: Yeah you can you tolower() too!


On Friday, 17 February 2012 at 23:50:32 UTC, Matt Soucy wrote:
> #!/usr/bin/rdmd
> import std.stdio;
> void main()
> {
>     uint guesses=0, high=100, low=0, guess=50;
>     char returned;
>     writef("Please choose a number between %s and %s.\n");
>     writef("Press enter to begin.",low,high);
>     readln();
>     checkLoop:
>     do {
>         guess = (high-low)/2+low; // Avoid overflow (shouldn't 
> occur)
>         writef("Is your number %s? [(y)es, (h)igher, (l)ower] 
> ", guess);
>         readf("%c", &returned);
>         readln();
>         switch(returned) {
>             case 'y','Y': break checkLoop;
>             case 'h','H': {low=guess; break;}
>             case 'l','L': {high=guess; break;}
>             default: break;
>         }
>     } while(++guesses);
>     writef("I guessed your number in %s moves!\n", guesses);
> }
>
>
> This piece is something I wrote quickly for /r/dailyprogrammer 
> (By the way, is the person who has been posting D solutions to 
> that on here?)
> It's a really simple piece, but it shows %s, breaking from a 
> loop from inside a switch, etc.




More information about the Digitalmars-d mailing list