`enum`

import std.stdio;
import std.conv;
enum Operation { exit, add, subtract, multiply, divide }
void main() {
write("Operations - ");
for (Operation operation;
operation <= Operation.max;
++operation) {
writef("%d:%s ", operation, operation);
}
writeln();
while (true) {
write("Operation? ");
int operationCode;
readf(" %s", &operationCode);
Operation operation = cast(Operation)operationCode;
if ((operation < Operation.min) ||
(operation > Operation.max)) {
writeln("ERROR: Invalid operation");
continue;
}
if (operation == Operation.exit) {
writeln("Goodbye!");
break;
}
double first;
double second;
double result;
write(" First operand? ");
readf(" %s", &first);
write("Second operand? ");
readf(" %s", &second);
switch (operation) {
case Operation.add:
result = first + second;
break;
case Operation.subtract:
result = first - second;
break;
case Operation.multiply:
result = first * second;
break;
case Operation.divide:
result = first / second;
break;
default:
throw new Exception(
"ERROR: This line should have never been reached.");
}
writeln(" Result: ", result);
}
}