Enumerated type variable

Example that uses an enumerated type variable.
This example uses enumerated type is used in C++ in exactly the same way it was used in ANSI-C with one small exception, the keyword enum is not required to be used again when defining a variable of that type, but it can be used if desired.

*     MYCPLUS Sample Code - http://www.mycplus.com     *
*                                                     *
*   This code is made available as a service to our   *
*      visitors and is provided strictly for the      *
*               purpose of illustration.              *
*                                                     *
* Please direct all inquiries to saqib at mycplus.com *


enum game_result {win, lose, tie, cancel};

game_result result;
enum game_result omit = cancel;

   for (result = win;result <= cancel;result++) {
      if (result == omit)
         cout << "The game was cancelled\n";
      else {
         cout << "The game was played ";
         if (result == win)
            cout << "and we won!";
         if (result == lose)
            cout << "and we lost.";
         cout << "\n";

// Result of execution
// The game was played and we won!
// The game was played and we lost.
// The game was played
// The game was cancelled

Leave a Reply