enum Solutions - D Programming Language

Menu

- Learn
- Documentation
  - Language Reference
  - Library Reference
  - Command-line Reference
  - Feature Overview
  - Articles
- Downloads
- Packages
- Community
  - Blog
  - Orgs using D
  - Twitter
  - Calendar
  - Discord (community-run)
  - Forums
  - IRC
  - Wiki
  - Issues
  - Contributing
  - Contributors
  - Foundation
  - Security Team
  - Donate
  - Sponsors
- Resources
  - Tour
  - Books
  - Tutorials
  - Tools
  - Editors
  - IDEs
  - run.dlang.io
  - Visual D
  - Acknowledgments
  - D Style
  - Glossary
  - Sitemap

Search
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

# enum Solutions

#### `enum` Dersi Problem Çözümleri

    import std.stdio;
    import std.conv;

    enum Operation { exit, add, subtract, multiply, divide }

    void main() {
        // Print the supported operations
        write("Operations - ");
        for (Operation operation;
             operation <= Operation.max;
             ++operation) {

            writef("%d:%s ", operation, operation);
        }
        writeln();

        // Unconditional loop until the user wants to exit
        while (true) {
            write("Operation? ");

            // The input must be read in the actual type (int) of
            // the enum
            int operationCode;
            readf(" %s", &operationCode);

            /* We will start using enum values instead of magic
             * constants from this point on. So, the operation code
             * that has been read in int must be converted to its
             * corresponding enum value.
             *
             * (Type conversions will be covered in more detail in
             * a later chapter.) */
            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);
        }
    }

Copyright © 1999-2026 by the D Language Foundation | Page generated by Ddoc on Wed Aug 5 15:38:50 2026