digitalmars.D - [your code here]
- Matt Soucy (28/28) Feb 17 2012 #!/usr/bin/rdmd
- MattCodr (9/40) Feb 17 2012 Nice, but just a little thing:
- Matt Soucy (7/46) Feb 17 2012 Thank you, however I feel that using the
- Jonathan M Davis (3/33) Feb 17 2012 Shouldn't it be dmd and not rdmd in the first line?
- Timon Gehr (2/35) Feb 17 2012 If you want it to be executable as a script, rdmd works just fine.
- Jonathan M Davis (5/6) Feb 17 2012 But is there any guarantee that rdmd will even be on the system? It seem...
- Matt Soucy (9/15) Feb 17 2012 Aah, whoops. I forgot about dmd -run.
- Andrei Alexandrescu (4/16) Feb 17 2012 I think rdmd can be safely assumed to be alongside dmd. If our
- Matt Soucy (7/40) Feb 17 2012 I chose rdmd because I wanted to demonstrate direct running it directly,...
- Jonathan M Davis (5/46) Feb 17 2012 As I understand it, you can run it as a script with #!/bin/dmd, and dmd ...
- Andrej Mitrovic (1/1) Feb 17 2012 Is this D1 code? You can't use writef with a format specifier and no arg...
- H. S. Teoh (6/8) Feb 17 2012 Probably a mistake, the extra arguments on the second writef() should be
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
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
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
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
On 02/18/2012 02:13 AM, Jonathan M Davis wrote:On Friday, February 17, 2012 18:50:32 Matt Soucy wrote:If you want it to be executable as a script, rdmd works just fine.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
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
On 02/17/2012 08:57 PM, Jonathan M Davis wrote:On Saturday, February 18, 2012 02:53:44 Timon Gehr wrote: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 SoucyIf 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
On 2/17/12 8:18 PM, Matt Soucy wrote:On 02/17/2012 08:57 PM, Jonathan M Davis wrote:I think rdmd can be safely assumed to be alongside dmd. If our installers don't do that, they're in error. AndreiOn Saturday, February 18, 2012 02:53:44 Timon Gehr wrote:Aah, whoops. I forgot about dmd -run.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
On 02/17/2012 08:13 PM, Jonathan M Davis wrote:On Friday, February 17, 2012 18:50:32 Matt Soucy wrote: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 Soucyimport 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
On Friday, February 17, 2012 21:03:43 Matt Soucy wrote:On 02/17/2012 08:13 PM, Jonathan M Davis wrote:guaranteed to be on your system if you're programming in D whereas rdmd might not be. - Jonathan M DavisOn Friday, February 17, 2012 18:50:32 Matt Soucy wrote: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...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
Is this D1 code? You can't use writef with a format specifier and no arguments.
Feb 17 2012
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