Jump to page: 1 2
Thread overview
[your code here]
Feb 17, 2012
Matt Soucy
Feb 18, 2012
MattCodr
Feb 18, 2012
Matt Soucy
Feb 18, 2012
Jonathan M Davis
Feb 18, 2012
Timon Gehr
Feb 18, 2012
Jonathan M Davis
Feb 18, 2012
Matt Soucy
Feb 18, 2012
Matt Soucy
Feb 18, 2012
Jonathan M Davis
Feb 18, 2012
Andrej Mitrovic
Feb 18, 2012
H. S. Teoh
February 17, 2012
#!/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.
February 18, 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.


February 18, 2012
On Friday, February 17, 2012 18:50:32 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.

Shouldn't it be dmd and not rdmd in the first line?

- Jonathan M Davis
February 18, 2012
On 02/18/2012 02:13 AM, Jonathan M Davis wrote:
> On Friday, February 17, 2012 18:50:32 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.
>
> Shouldn't it be dmd and not rdmd in the first line?
>
> - Jonathan M Davis

If you want it to be executable as a script, rdmd works just fine.
February 18, 2012
On Saturday, February 18, 2012 02:53:44 Timon Gehr wrote:
> If you want it to be executable as a script, rdmd works just fine.

But is there any guarantee that rdmd will even be on the system? It seems much safer to me to use dmd, since dmd _will_ be there if you're programming in D, but rdmd may or may not be there.

- Jonathan M Davis
February 18, 2012
Thank you, however I feel that using the
case 'y','Y':
demonstrates another feature that C/C++ doesn't have. I'm aware of those two functions.
...although in the middle of testing this I realized that "guesses=0" should have been "guesses=1". Whoops.

On 02/17/2012 08:07 PM, MattCodr wrote:
> 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.
>
>
February 18, 2012
On 02/17/2012 08:13 PM, Jonathan M Davis wrote:
> On Friday, February 17, 2012 18:50:32 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.
>
> Shouldn't it be dmd and not rdmd in the first line?
>
> - Jonathan M Davis

I chose rdmd because I wanted to demonstrate direct running it directly, not building it that way.
Out of curiosity, though, is there a specific reason why I should have used dmd instead? Without switches, that would just build it...
Thank you,
-Matt Soucy
February 18, 2012
On 02/17/2012 08:57 PM, Jonathan M Davis wrote:
> On Saturday, February 18, 2012 02:53:44 Timon Gehr wrote:
>> If you want it to be executable as a script, rdmd works just fine.
>
> But is there any guarantee that rdmd will even be on the system? It seems much
> safer to me to use dmd, since dmd _will_ be there if you're programming in D,
> but rdmd may or may not be there.
>
> - Jonathan M Davis
Aah, whoops. I forgot about dmd -run.
I understand now, sorry and thank you. It would be better to use that. I was only slightly mixed up with how arguments are split in a shebang line (on my system, "#!x 1 2 3" is split as "x" "1 2 3" followed by the name of the file, not "x 1 2 3" as I had thought it had been, nor "x" "1" "2" "3")
Thank you for explaining your problem.
-Matt Soucy
February 18, 2012
On Friday, February 17, 2012 21:03:43 Matt Soucy wrote:
> On 02/17/2012 08:13 PM, Jonathan M Davis wrote:
> > On Friday, February 17, 2012 18:50:32 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.
> > 
> > Shouldn't it be dmd and not rdmd in the first line?
> > 
> > - Jonathan M Davis
> 
> I chose rdmd because I wanted to demonstrate direct running it directly,
> not building it that way.
> Out of curiosity, though, is there a specific reason why I should have
> used dmd instead? Without switches, that would just build it...

As I understand it, you can run it as a script with #!/bin/dmd, and dmd is guaranteed to be on your system if you're programming in D whereas rdmd might not be.

- Jonathan M Davis
February 18, 2012
Is this D1 code? You can't use writef with a format specifier and no arguments.
« First   ‹ Prev
1 2