Enum Loop question

I'm trying to figure out how to loop through enums. The code below doesn't work, but is an example of the concept I'm trying to work through:

1
2
3
4
5
enum DIRECTION {NORTH, SOUTH, EAST, WEST};
for (DIRECTION d=NORTH; d<4; ++d)
{
  //Loop through each direction and do something
}


The problem I have is that the ++d is giving me the compiler error; I know it has to do with not being able to convert int types to enums, even though it works the other way around.

My question is how do I get around this? Can I create an operator overload like I do in classes to allow addition for my loop to work? Or is there an easy way to loop through enums that I'm not aware of?
//Loop through each direction and do something

But what are you trying to do?

The use for an enumerator is to define a type that you would otherwise have to keep a "magic number" for. You also receive type safety:
1
2
3
4
5
6
7
8
9
10
enum colors_t {black, blue, green, cyan, red, purple, yellow, white};

void myFunc(colors_t color)
{
  switch (color)
  {
  case black: /* ... */ break;
  // ...
  }
}


Not exactly sure what use the actual integral value is (but we can define it, so there must be one).

Last edited on
Ok.. I figured it out..

1
2
3
4
5
enum DIRECTION {NORTH, SOUTH, EAST, WEST};
for (int d=NORTH; d<4; ++d)
{
  DIRECTION thisDirection = static_cast<DIRECTION>(d);
}



LowestOne... thanks for the attempt.. what i'm doing is writing a "Lights Out" puzzle game. I'm testing out a game engine I'm writing. I got it working, but you can see at the bottom of this code where I'm using the enum loop...

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
#include "../../../Libraries/HXPA5v1_0_GameEngine.h"

int main()
{
	Engine.HideConsole();
	WINDOW win;
	win.Initialize(400,300,"Lights Out", (Engine.GetScreenWidth()/2)-200,(Engine.GetScreenHeight()/2)-150);
	win.Clear(Colors.Black);

	GRID grid;
	IMAGE img;
	bool Cells[25];

	grid.Initialize(32,32,5,5);
	img.Create(32*5,32*5);
	img.DefineCells();

	//Build grid image
	for (int i=0; i<grid.GetCellCount(); ++i)
	{
		Cells[i] = false;
	}
	//Set default starting game
	Cells[12] = true;


	bool gameRunning = true;
	bool redraw = true;

	while (gameRunning)
	{
		if (redraw == true)
		{
			//Color grid cells appropriately
			img.Focus();
			img.Clear(Colors.LightGray);
			for (int i=0; i<grid.GetCellCount(); ++i)
			{
				img.Rect(i).Draw(Colors.Black);
				if (Cells[i] == true) { img.Rect(i).Draw(Colors.Yellow,true); }
				else { img.Rect(i).Draw(Colors.Black,false); } 
			}
			//Return focus to the window and draw the grid image
			win.Focus();
			img.Draw(img.Rect(),Point(200-(img.GetWidth()/2),150-(img.GetHeight()/2)));
			win.Refresh();
			redraw = false;
		}
		//Prepare to receive user input
		win.DoEvents();

		int input = win.GetInput();

		switch (input)
		{
		case WINDOW_CLOSE:
			gameRunning = false;
			break;
		case MOUSE_DOWN_LEFT:
			int aCell = grid.GetCell(Point(win.Mouse.Pt.X - (200-(img.GetWidth()/2)),	win.Mouse.Pt.Y - (150-(img.GetHeight()/2))));
			if (aCell == -1) break; //Invalid or out of bounds cell
			Cells[aCell] = !Cells[aCell];
			for (int j=NORTH; j<4; j++)
			{
				int neighbor = grid.GetNeighbor(aCell,static_cast<DIRECTION>(j));
				if (neighbor != -1) Cells[neighbor] = !Cells[neighbor];
			}
			redraw = true;
			break;
		}
	}

	win.Destroy();
	return 0;
}
Last edited on
But why are you doing that?

I said this before HellfireXP's edit
Last edited on
For fun.
Topic archived. No new replies allowed.