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

Reply via email to