gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
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:
enum enum_name { constant1, constant2, constant3, ... };Example:
#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; }In the above example, we have declared an enum named Color with three named constants: RED, BLUE, and GREEN. We then declare a variable named c of type Color and assign the value RED to it. We then check the value of c using if-else statements to print the corresponding color name.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.