< previous page page_541 next page >

Page 541
The only caution here is that when control exits the loop, the value of patient is one greater than the largest value in the domain (SHEEP). If you want to use patient outside the loop, you must reassign it a value that is within the appropriate range for the Animals type.
Comparison
The most common operation performed on values of enumeration types is comparison. When you compare two values, their ordering is determined by the order in which you listed the enumerators in the type declaration. For instance, the expression
inPatient <= BIRD
is TRUE if inPatient contains the value RODENT, CAT, DOG, or BIRD.
You can also use values of an enumeration type in a Switch statement. Because RODENT, CAT, and so on are literals, they can appear in case labels:
switch (inPatient)
{
    case RODENT  :
    case CAT     :
    case DOG     :
    case BIRD    : cout < Cage ward;
                   break;
    case REPTILE : cout < Terrarium ward;
    case HORSE   :
    case BOVINE  :
    case SHEEP   : cout <Barn;
}
Input and Output
Stream I/O is defined only for the basic built-in types (int, float, and so on), not for enumeration types. Values of enumeration types must be input or output indirectly.
To input values, the usual strategy is to read a number or a letter code and translate it to one of the identifiers in the enumeration type. For example, the veterinary office program could read the kind of animal as a series of characters, then assign one of the values of type Animals to that patient. The following program fragment reads in an animal represented by its first two letters and converts it to one of the values in type Animals.
cin >> ch1 >> ch2;
switch (ch1)
{
    case R : if (ch2 == o)
                   inPatient = RODENT;

 
< previous page page_541 next page >