Programming in D – Solutions

The Ternary Operator ?:

Although it may make more sense to use an if-else statement in this exercise, the following program uses two ?: operators:

import std.stdio;

void main() {
    write("Please enter the net amount: ");

    int amount;
    readf(" %s", &amount);

    writeln("$",
            amount < 0 ? -amount : amount,
            amount < 0 ? " lost" : " gained");
}

The program prints "gained" even when the value is zero. Modify the program to print a message more appropriate for zero.