Enums

Could someone explain what are enums, and how do we use them? The Description in the site's tutorial is too short to properly understand how it works.
Thanks in Advance.
Last edited on
I'll probably be regurgitating the tutorials on this site but I'll try to elaborate things further for you.

Enumerations allow you to create you own data type within the constructs of your application. Unlike predefined data types you can determine the scope and the significance of the custom type.

For example if you wanted to express different types of "Car Tyres" in your programming code you could use an Enumeration for this. For example:

enum CarTyre { normal, wet, snow, sports }; //for the sake of a later example please not giraffe is not included in this list

Now with your Enumeration defined you can use it, just as you would an int, bool, string or any other data type. For example:

1
2
3
int     nNumber     = 0;
bool    fIsValid    = false;
CarTyre ctFrontLeft = normal;


For the sake of completeness I will include these incorrect usages:

1
2
3
int     nNumber     = "A"      //as the character "A" is not a valid integer;
bool    fIsValid    = 1.42;    //as 1.42 is a floating point value and not a boolean
CarTyre ctFrontLeft = giraffe  //as giraffe is not a valid CarTyre (these were specified in our enum statement earlier); 


You can also apply logic operations, for example:

1
2
3
4
5
6
7
8
if( nNumber == 1 )
    //do something

if( fIsValid == false )
    //do something

if( ctFrontLeft == normal )
    //do something 


I hope this helps.
Enum's let you define your own data types that can only have the specific values you specify. For example the variable type bool could be expressed as (of course bool is a keyword and you couldn't actually do this, but bear with me for the sake of example)
 
enum bool { FALSE, TRUE };

Enum's are indexed starting at 0 and incrementing by one for each value (how odd that 0 means false and 1 means true in C++, and these are the indexes of true and false in the enum above :o). You can specify otherwise if you want to however by assigning elements a specific index.
 
enum bool { FALSE = 1, TRUE = 0 };

You can also assign integers to enums like this:
 
int test = TRUE;

If the enum bool was defined as I defined it in the original example, test would hold a value of 1.

You asked how it's used so here's an example. Let's say you were making a 2D game, and the player in it must be facing in one of four directions at all times, up, down, left and right. Then you could define an enum like this:
 
enum playerDirection { UP, DOWN, LEFT, RIGHT} player;            // remember you can define objects by putting them before the semicolon at the end :-) 

Then you're player will always be facing one direction, and you can change the direction to whatever you want. For example if the person playing the game pressed the right arrow key you could say
 
player = RIGHT;

You could also say
 
player = 3;


These would both have the same affect :0. Hope this helps.

Also you might want to take a good read here: http://www.cprogramming.com/tutorial/enum.html

EDIT: Dangit someone posted before me!
Last edited on
A key feature about enum that I use a lot is the fact that it assigns numerical values to the new data type. This allows me to do stuff like this:
1
2
3
4
5
6
7
8
9
10
11
enum _SUIT {Heart, Diamond, Club, Spade}Suit, *PSuit;

//... code code code

Suit CS = Diamond;

switch(CS) //switch requires a data type that can be cast to an integer which is what enum does.
{
    case Heart: /*....*/ break;
    case Daimond: /*...*/ break;
//YOU GET THE PICTURE 

This avoids the "if(...){...} else if(...){...} else if(...){...}" crap that you see so often.
OK now I get the idea behind Enums.
But I am havis one problem using it.
I was making a program that will store info about a Person. So I made a struct Person
I then decided to have Gender as one of the info, and tried to use enums. The code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
enum Gender
{
	Male,Female
};

struct Person 
{
	int IDNUM;
	string Name;
	int Age;
	int Height;
	Gender gender;
	string Profession;
};


Now when I try to get input from the user using:
cin >> person.gender;
I get the error:
error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)
Can someone tell me the problem with the way I am using it?

EDIT:
@Computergeek01:
switch requires a data type that can be cast to an integer which is what enum does.

I am not so sure about that since I have used chars and strings in it as well.
Last edited on
Char's and strings can be converted to their ASCII values, which are integers. They are being casted as integers.

As for your error, did you forget a header file?
Last edited on
Ah, well, that explains it!

But what about the error I was receiving?
Can you post your whole program? The only thing I can think of is that you forgot a header file.
This is the program:
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
#include <iostream>
#include <sstream>
#include <cstring>
#include <fstream>

using std::cout;
using std::cin;
using std::endl;
using std::string;

enum Gender
{
	Male,Female
};

struct Person 
{
	int IDNUM;
	string Name;
	int Age;
	int Height;
	Gender gender;
	string Profession;
};

void PersonData (Person &person)
{
	cout << "Please Enter the following details of the Person: " << endl;
	
	cout << "Name:  ";
	cin >> person.Name;
	
	cout << "Age:  ";
	cin >> person.Age;

	cout << "Height (in cm):  ";
	cin >> person.Height;

	cout << "Gender:  ";
	cin >> person.gender; 		//The Error is on this line. It also persists if I use getline

	cout << "Profession:  ";
	getline (cin,person.Profession);;
}
closed account (zb0S216C)
Enumerated structures are always constant, therefore, you cannot reassign new values after the declaration. Also, why are you trying to input a structure? That doesn't make any sense. Even if you changed that, it still won't work.

Wazzak
Last edited on
Uhh that's weird, I can't think of anything wrong. The only quick fix I can think of is to change your input to something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char mf;
std::cout << "M/F:";
std::cin >> mf;

switch(gender)
{
	case 'm":
	case 'M':
		person.gender = 0;
	case 'f':
	case 'F':
		person.gender = 1;
	default:
		std::cout << "Invalid gender.  Enter either M or F" << std::endl;
}
 
@ascii: Thanks that worked!

@Framework:
But where am I reassigning new values to the Gender enum after its deceleration, I am trying to a assign a value to a object gender of type Gender
closed account (zb0S216C)
Nisheeth wrote:
But where am I reassigning new values to the Gender enum after its deceleration, I am trying to a assign a value to a object gender of type Gender

On line 40. You prompt the user to enter a structure, which doesn't make sense. For example:

1
2
cin >> Person.gender; // What? gender is an instance of Gender which is a structure of constant values.
cin >> Person.gender.Male; // Error. Male is a constant. 

Wazzak
Last edited on
Ah! my bad!
Thanks for the explanation.
Topic archived. No new replies allowed.