Enumerated types

Enumerated types list all the possible values a type can take, either as a set of strings or as a range of values

enum booleflag { false, true };
booleflag = true;
In this example, booleflag is the name of the type, and it's acceptable values are false or true.

The specific values (false, true) are matched internally to a sequence of integers: 0..N-1 where N is the number of itemsin the list

Thus the names within the enumerated type can be used on their own, or they can be treated as an integer value. Hence they can even be used the as array indices, e.g. myarray[false] in this case would be the same as typing myarray[0] and myarray[true] would be the same as myarray[1].

Thus internally they are just integers, and we can use them that way if we want. However, for the purposes of type checking, they effectively allow us to limit the range of possible values to a small set of keywords.

For instance, if we wanted to create a list of names for the months of the year, and specify that the elements from that list are a valid data type on their own, we could do the following

enum ValidMonths {
   January=1, February=2, March=3, April=4, May=5, June=6,
   July=7, August=8, September=9, October=10, November=11, December=12
};
Now, to use these months in a program, the name has an integer value associated with it. For example:
ValidMonths thismonth;  // variable to store the current month
char input[MAXSIZE];

cout << "Please enter the month" << endl;
cin >> input;
if (strcmp(input, "January") == 0) thismonth = January;
else if (strcmp(input, "February") == 0) thismonth = February;
//
// ... and more for the other months
//
else if (strcmp(input, "December") == 0) thismonth = December;
else cout << "Invalid month: " << input << endl;

// now print out the number for the month
cout << thismonth << endl;

   // if the user had entered "January" this would display 1,
   // if they'd entered "February" it would display 2, etc