enums

I still do not know about where I can use enums,please give me advice or teach me..I always think that why I have to use enums if I could declare many variables as many as in enums
Enums are often used to store lists of options, like the current state of something. A couple of examples of enums from a game I've been making recently include status effect names and (internally) storing the type of an item, used for casting. For example, for status effects:
1
2
3
4
5
6
7
8
enum class Status : unsigned int {
    Burn   = 1 << 0,
    Poison = 1 << 1,
    Sleep  = 1 << 2,
    // etc...
};
// Then I can meld them all together with bit operations. I did
// provide a wrapper class as well, but that is the basic function. 
Last edited on
http://en.cppreference.com/w/cpp/language/enum

Variables use memory. Enums are named constants with distinct type.

Therefore, (global) constants are somewhat similar to enums, but with enums you can have more visibility and type-check control.

I'd say that they are less need and more style, just like naming variables can make code harder or easier to understand.
@mutexe I still dont get it :(
@NT3 can you give me an easier example like the colors...
1
2
3
4
5
6
7
8
9
10
11
12
13
enum Colors = 
{ BLACK = 0, 
  BLUE = 1, 
  GREEN = 2, 
  CYAN = 3, 
  RED = 4, 
  MAGENTA = 5, 
  BROWN = 6, 
  LT_GRAY = 7,
};

set_text_color (CYAN); 
set_text_color (3);

Which of the two lines is clearer?
NT3 I hope you're not a teacher :)
No. And its probably a good thing, I just gave an example I was actually using at the time.
@AbstractionAnon the first one. but i still dont know where i can use enums in my program.

You can use an enum any place a name for a number makes the code clearer.
@Lorence30

Here's a small program I wrote that uses enum to easily change text colors on screen, by using its name instead of numbers. As AbstractionAnon says, makes the code a lot clearer on what's happening and why.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Colored Text on Paper.cpp : main project file.

#include <iostream>
#include <string>
#include <conio.h> // For _kbhit()) _getch() use
#include <windows.h>

enum Colors{
	black,          //  0 text color - multiply by 16, for background colors
	dark_blue,      //  1
	dark_green,     //  2
	dark_cyan,      //  3
	dark_red,       //  4
	dark_magenta,   //  5
	dark_yellow,    //  6
	light_gray,     //  7
	dark_gray,      //  8
	light_blue,     //  9
	light_green,    // 10
	light_cyan,     // 11
	light_red,      // 12
	light_magenta,  // 13
	light_yellow,   // 14
	white           // 15
};


using namespace std;

void ClearScreen();
void WaitKey();

#define on , // So I can use the function - void text(text_color on background_color)
             // To more easily remember which is text color vs background color

// My text color function. Use it if you wish.
void text(int text_color = 7 on int paper_color = 0)
{
	// defaults to light_gray on black

	int color_total = (text_color+(paper_color*16));
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color_total );
}

int main()
{
	string Color[16] ={
		"Black","Dark Blue","Dark Green","Dark Cyan",
		"Dark Red","Dark Magenta","Dark Yellow","Light Gray",
		"Dark Gray","Light Blue","Light Green","Light Cyan",
		"Light Red","Light Magenta","Light Yellow","White"
	};
	int i,j;

	for(j=0;j<16;j++)
	{
		for(i=0;i<16;i++)
		{
			if(j==i) // If text color is same as paper color
			{
				text();
				cout << "--> *** Place-holder --- " << Color[i] << " on " << Color[j] << " *** <--" << endl;
				i++; // Skip to next color
			}
			if (i<16) // If not white on white, then continue
			{
				text(i on j);
				cout << Color[i] << " on " << Color[j] << " (Text color=" << i << 
					"+Background color="<< j*16 << " (" << j << "*16)=" << i+(j*16) << ")" << endl;
			}
		}
	}
	text();

	cout << endl <<"To use color, add ";
	text(light_red on black); // Able to use names here, because of enum Colors{};
	cout << "#include <windows.h>";
	text();
	cout << " to your list of ";
	text(light_red on black);
	cout << "#include";
	text();
	cout << "'s, " << endl;
	cout << "if not already used.. Then, declare the following, afterwards.." << endl;
	text(light_red on black);
	cout << "HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);" << endl;
	text();
	cout << "and use the following, whenever you wish to change colors.." << endl;
	text(light_red on black);
	cout << "SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color_total );" << endl;
	text();
	cout <<  "Or examine the '";
	text(light_red on black);
	cout << "void text(int text_color on int paper_color);";
	text();
	cout << "' function," << endl <<"at the top of this program. Works great, doesn't it ??" << endl;
	cout << "Just make sure to add '";
	text(light_red on black);
	cout << "#define on ,";
	text();
	cout << "', if you want to use the '";
	text(light_red on black);
	cout << "on";
	text();
	cout << "' word," << endl;
	cout << "instead of a comma, to separate the two color choices." << endl << endl;
	cout << "Press enter to exit.." << endl;
	
	WaitKey();
	ClearScreen();
	cout << "\n\n\n\t\tHope you enjoyed.." << endl;
	return 0;
}

void ClearScreen()
  {
   DWORD n;
  DWORD size;
  COORD coord = {0};
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
  GetConsoleScreenBufferInfo ( h, &csbi );
  size = csbi.dwSize.X * csbi.dwSize.Y;
  FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
  GetConsoleScreenBufferInfo ( h, &csbi );
  FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
  SetConsoleCursorPosition ( h, coord );
  }

void WaitKey()
{
	while (_kbhit()) _getch(); // Empty the input buffer
	_getch(); // Wait for a key
	while (_kbhit()) _getch(); // Empty the input buffer (some keys sends two messages)
}
can you translate your code into non-enums which make your codes not clear?
Are you serious?
Why would whitenite1 want to do that?

If you're that curious about it, you can do that yourself using an editor to change all occurances of black to 0, dark_blue to 1, etc.
sorry i just want to see the difference.... but now i gained more ideas about enums. let me close this thread temporarily okay? thank you all
Topic archived. No new replies allowed.