C++ Enumerated type variable [C++ Enum]

trie data structure

This C++ program shows how to use enumerated type . The enum is a compound data type and works in C++ exactly the way it works in ANSI-C with one small exception. The keyword enum is not required to use when defining a variable of the enum type, but it can be used if desired.

#include <iostream>

using namespace std;
 
enum game_result {win, lose, tie, cancel};
 
int main(void)
{
 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";
  }
 }
}

Output of the C++ Program:

The game was played and we won!

The game was played and we lost.

The game was played

The game was cancelled

M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post