//enumio.cpp -- Examples of reading and writing enumeration literals

#include <iostream>
#include <string>


enum ageType {DRIVER=16, DRINKING=21, RETIREMENT=65};

enum {AGECARDINALITY = 3}; // The purpose of this declaration is just to
                           // introduce the constant AGECARDINALITY with
                           // value 3, i.e. the number of values of
                           // the type ageType

const char * const ids[] = {"DRIVER","DRINKING","RETIREMENT"};
const ageType enums[] = {DRIVER, DRINKING, RETIREMENT};

//Obtain from input an ageType literal (keep on asking until right)
ageType readAge(void){
   string id;

   while (1){
      cout << "Enter an age [DRIVER|DRINKING|RETIREMENT]: ";
      cin >> id;
      for (int lcv = 0; lcv < AGECARDINALITY; lcv++){
          if (id == ids[lcv]){
             return enums[lcv];
          }
      }
   }
}

//Return an ageType value as a character array of the corresponding
//enumeration literal
const char * returnAge(ageType age){
   for (int lcv = 0; lcv < AGECARDINALITY; lcv++){
       if (age == enums[lcv])
          return ids[lcv];
   }
   return NULL;  // NULL is used to represent a pointer to nothing
}

void main(void){
   for (int lcv = 0; lcv < AGECARDINALITY; lcv++){
       ageType who = readAge();
       cout << "\nThe literal you entered is " << returnAge(who) 
            << " with value " << int(who) << endl;
   }
}
