www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - short programme from "Programming in D" by Ali Cehreli.

reply Nick B <nick.barbalich gmail.com> writes:
Hi, Can anyone assist me with the short D programme. It is taken 
straight from  "Programming in D" by Ali Cehreli.  The code below 
is the solution,  on page 684.
The  Exercise is on Page 27, item 2 half way down the page.

The problem is that the program crashes, when it runs.   The only 
thing I have changed is the placement of the curly brackets on 
new lines, otherwise the code is the same.  My text editor is 
Sublime 3 - Build 3114.

DMD32 D Compiler v2.069.2
Running on Win 7
result:
Microsoft Windows [Version 6.1.7601]
DMD32 D Compiler v2.069.2

C:\Users\Joan\Prog\Source>page27b
Roll the dice .....
3

object.Exception C:\Users\Joan\Prog\D\dmd2\windows\bin\..\..\src\phobos\std\form
at.d(594): Enforcement failed
----------------
0x00403849
0x004037F4
0x0040251F
0x00402472
0x0040858F
0x00408553
0x00408454
0x00404C83
0x76DA338A in BaseThreadInitThunk
0x777B9902 in RtlInitializeExceptionChain
0x777B98D5 in RtlInitializeExceptionChain

C:\Users\Joan\Prog\Source>

***************************************************

Here is all the source:

import std.stdio;
void main ()
{
	writeln  ("Roll the dice .....");
	int die ;
	readf (" %s, &die");

	if ((die == 1) || (die == 2) || (die == 3))
		{
			writeln("You won");
		}
	else if ((die == 4) || (die == 5) || (die == 6))
		{
			writeln("I won");
		}
	else
		{
			writeln("ERROR: ", die , "is invalid");
		}
}


Note, this source below, take from the code samples .zip file 
works correctly !!

module if_solution_3;

import std.stdio;

void main() {
     write("What is the value of the die? ");
     int die;
     readf(" %s", &die);

     if ((die == 1) || (die == 2) || (die == 3)) {
         writeln("You won");

     } else if ((die == 4) || (die == 5) || (die == 6)) {
         writeln("I won");

     } else {
         writeln("ERROR: ", die, " is invalid");
     }
}

with Thanks
Nick
Jun 14 2016
parent reply cym13 <cpicard openmailbox.org> writes:
On Tuesday, 14 June 2016 at 09:17:35 UTC, Nick B wrote:
 Hi, Can anyone assist me with the short D programme. It is 
 taken straight from  "Programming in D" by Ali Cehreli.  The 
 code below is the solution,  on page 684.
 The  Exercise is on Page 27, item 2 half way down the page.

 [...]
Misplaced quote in your readf.
Jun 14 2016
parent reply Nick B <nick.barbalich gmail.com> writes:
On Tuesday, 14 June 2016 at 09:28:18 UTC, cym13 wrote:
 On Tuesday, 14 June 2016 at 09:17:35 UTC, Nick B wrote:
 Hi, Can anyone assist me with the short D programme. It is 
 taken straight from  "Programming in D" by Ali Cehreli.  The 
 code below is the solution,  on page 684.
 The  Exercise is on Page 27, item 2 half way down the page.

 [...]
Misplaced quote in your readf.
Thank you. Question: is this a compiler bug ?
Jun 14 2016
parent reply cym13 <cpicard openmailbox.org> writes:
On Tuesday, 14 June 2016 at 09:39:05 UTC, Nick B wrote:
 On Tuesday, 14 June 2016 at 09:28:18 UTC, cym13 wrote:
 On Tuesday, 14 June 2016 at 09:17:35 UTC, Nick B wrote:
 Hi, Can anyone assist me with the short D programme. It is 
 taken straight from  "Programming in D" by Ali Cehreli.  The 
 code below is the solution,  on page 684.
 The  Exercise is on Page 27, item 2 half way down the page.

 [...]
Misplaced quote in your readf.
Thank you. Question: is this a compiler bug ?
No, why would it? You gave it a string saying "Hey, the store the data in the next variable" then gave no such variable. Format detected it and rightfully threw an error. That's the following line from your paste: object.Exception C:\Users\Joan\Prog\D\dmd2\windows\bin\..\..\src\phobos\std\form at.d(594): Enforcement failed It then printed as much of a stack trace as it could, not very useful as you didn't do much but it's there. You gave it garbage and it threw an error. Compiler bugs are a reality but not *that* common ;-)
Jun 14 2016
parent reply Nick B <nick.barbalich gmail.com> writes:
On Tuesday, 14 June 2016 at 09:53:03 UTC, cym13 wrote:
 On Tuesday, 14 June 2016 at 09:39:05 UTC, Nick B wrote:
 On Tuesday, 14 June 2016 at 09:28:18 UTC, cym13 wrote:
 On Tuesday, 14 June 2016 at 09:17:35 UTC, Nick B wrote:
 Hi, Can anyone assist me with the short D programme. It is 
 taken straight from  "Programming in D" by Ali Cehreli.  The 
 code below is the solution,  on page 684.
 The  Exercise is on Page 27, item 2 half way down the page.
[snip]
It then printed as much of a stack trace as it could, not very useful as you didn't do much but it's there. You gave it garbage and it threw an error. Compiler bugs are a reality but not *that* common ;-)
Thanks for the assistance. I assumed that the compiler would at least throw a line number, to hint at where the problem was.
Jun 14 2016
next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/14/16 7:52 AM, Nick B wrote:
 On Tuesday, 14 June 2016 at 09:53:03 UTC, cym13 wrote:
 On Tuesday, 14 June 2016 at 09:39:05 UTC, Nick B wrote:
 On Tuesday, 14 June 2016 at 09:28:18 UTC, cym13 wrote:
 On Tuesday, 14 June 2016 at 09:17:35 UTC, Nick B wrote:
 Hi, Can anyone assist me with the short D programme. It is taken
 straight from  "Programming in D" by Ali Cehreli.  The code below
 is the solution,  on page 684.
 The  Exercise is on Page 27, item 2 half way down the page.
[snip]
It then printed as much of a stack trace as it could, not very useful as you didn't do much but it's there. You gave it garbage and it threw an error. Compiler bugs are a reality but not *that* common ;-)
Thanks for the assistance. I assumed that the compiler would at least throw a line number, to hint at where the problem was.
You should see a stack trace. I'm not normally on windows, so I don't know the proper mechanism to enable stack traces, but on OSX, it looks like this: object.Exception /Users/steves/.dvm/compilers/dmd-2.071.0/osx/bin/../../src/phobo /std/format.d(596): Trailing characters in formattedRead format string ---------------- 4 testdice 0x00000001013a2357 pure safe void std.exception.bailOut!(Exception).bailOut(immutable(char)[], ulong, const(char[])) + 123 5 testdice 0x00000001013a22d0 pure safe bool std.exception.enforce!(Exception, bool).enforce(bool, lazy const(char)[], immutable(char)[], ulong) + 100 6 testdice 0x00000001013a0978 uint std.format.formattedRead!(std.stdio.LockingTextReader, char).formattedRead(ref std.stdio.LockingTextReader, const(char)[]) + 124 7 testdice 0x00000001013a08b9 uint std.stdio.File.readf!().readf(const(char[])) + 197 8 testdice 0x00000001013a07ea uint std.stdio.readf!().readf(const(char[])) + 38 9 testdice 0x00000001013a0264 _Dmain + 52 10 testdice 0x00000001013c07eb D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv + 39 11 testdice 0x00000001013c071f void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate()) + 35 12 testdice 0x00000001013c0790 void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll() + 44 13 testdice 0x00000001013c071f void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate()) + 35 14 testdice 0x00000001013c0685 _d_run_main + 497 15 testdice 0x00000001013a037f main + 15 16 libdyld.dylib 0x00007fff987e75ac start + 0 17 ??? 0x0000000000000000 0x0 + 0 No line numbers, but at least I can see the call stack. -Steve
Jun 14 2016
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 06/14/2016 04:52 AM, Nick B wrote:

 Thanks for the assistance. I assumed that the compiler  would at least
 throw a line number, to hint at where the problem was.
Further, when the format string is a literal like the one used in the program, the compiler can in theory determine at compile time that the format string does not match the rest of the arguments: readf (" %s, &die"); // "No argument for %s" This is a desired feature but dmd does not have this yet. Since dmd provides the front end to gdc and ldc, they don't have this feature either. Ali
Jun 14 2016
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/14/16 11:44 AM, Ali Çehreli wrote:
 On 06/14/2016 04:52 AM, Nick B wrote:

 Thanks for the assistance. I assumed that the compiler  would at least
 throw a line number, to hint at where the problem was.
Further, when the format string is a literal like the one used in the program, the compiler can in theory determine at compile time that the format string does not match the rest of the arguments: readf (" %s, &die"); // "No argument for %s" This is a desired feature but dmd does not have this yet. Since dmd provides the front end to gdc and ldc, they don't have this feature either.
Hm... shouldn't it at least require ONE parameter besides the string? -Steve
Jun 14 2016
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 06/14/2016 09:09 AM, Steven Schveighoffer wrote:

     readf (" %s, &die");
     // "No argument for %s"

 This is a desired feature but dmd does not have this yet. Since dmd
 provides the front end to gdc and ldc, they don't have this feature
 either.
Hm... shouldn't it at least require ONE parameter besides the string? -Steve
Good idea in this case but I think the real issue is providing this support for user-defined functions as well. bool argument_check_function(/* ... */) { // ... } pragma(arg_check, argument_check_function) int foo(int, string); I guess it's possible to do something with UDAs as well but the check must happen at call sites, which I think UDAs can't reach. Ali
Jun 14 2016
prev sibling parent Nick B <nick.barbalich gmail.com> writes:
On Tuesday, 14 June 2016 at 16:09:00 UTC, Steven Schveighoffer 
wrote:
 On 6/14/16 11:44 AM, Ali Çehreli wrote:
 On 06/14/2016 04:52 AM, Nick B wrote:
 Further, when the format string is a literal like the one used 
 in the
 program, the compiler can in theory determine at compile time 
 that the
 format string does not match the rest of the arguments:

     readf (" %s, &die");
     // "No argument for %s"

 This is a desired feature but dmd does not have this yet. 
 Since dmd
 provides the front end to gdc and ldc, they don't have this 
 feature either.
I will log a feature request with Walter. Nick
Jun 15 2016