gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
How to use enum in c++?
- User
How to use enum in c++?
- Assistant
An enum in C++ is a keyword that is used to declare an enumerated data type. It is used to define a set of named integer constants. Enum helps in making the code more readable, as it replaces the hard-coded numeric values with descriptive names. Here is how to use enum in C++:
Syntax:
cppenum enum_name { constant1, constant2, constant3, ... };Example:
cpp#include <iostream> using namespace std; // Declare an enum named Color with 3 named constants enum Color {RED, BLUE, GREEN}; int main() { // Declare a variable of Color type Color c = RED; // Print the value of the enum variable if (c == RED) { cout << "The color is RED." << endl; } else if (c == BLUE) { cout << "The color is BLUE." << endl; } else if (c == GREEN) { cout << "The color is GREEN." << endl; } return 0; }