www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [your code here]

reply Matt Soucy <msoucy csh.rit.edu> writes:

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.
Feb 17 2012
next sibling parent reply "MattCodr" <mattcoder hotmail.com> writes:
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:

 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.
Feb 17 2012
parent Matt Soucy <msoucy csh.rit.edu> writes:
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:

 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.
Feb 17 2012
prev sibling parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Friday, February 17, 2012 18:50:32 Matt Soucy wrote:

 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
Feb 17 2012
next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 02/18/2012 02:13 AM, Jonathan M Davis wrote:
 On Friday, February 17, 2012 18:50:32 Matt Soucy wrote:

 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.
Feb 17 2012
parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
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
Feb 17 2012
parent reply Matt Soucy <msoucy csh.rit.edu> writes:
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 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
Feb 17 2012
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 2/17/12 8:18 PM, Matt Soucy wrote:
 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 think rdmd can be safely assumed to be alongside dmd. If our installers don't do that, they're in error. Andrei
Feb 17 2012
prev sibling parent reply Matt Soucy <msoucy csh.rit.edu> writes:
On 02/17/2012 08:13 PM, Jonathan M Davis wrote:
 On Friday, February 17, 2012 18:50:32 Matt Soucy wrote:

 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
Feb 17 2012
next sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
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:

 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...
guaranteed to be on your system if you're programming in D whereas rdmd might not be. - Jonathan M Davis
Feb 17 2012
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Is this D1 code? You can't use writef with a format specifier and no arguments.
Feb 17 2012
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Feb 18, 2012 at 03:38:59AM +0100, Andrej Mitrovic wrote:
 Is this D1 code? You can't use writef with a format specifier and no
 arguments.
Probably a mistake, the extra arguments on the second writef() should be moved to the first write(). T -- When solving a problem, take care that you do not become part of the problem.
Feb 17 2012