user2234688
user2234688

Reputation: 79

Displaying enumerated type

Alright so this is my display function, and the enumerated type associated with it.

enum EventType {ARRIVAL = 'A', DEPARTURE = 'D'};    


void EventList::display()
{
cout << "Event List: ";
for (ListNode *cur = head; cur != NULL; cur = cur->next)
{
    if (cur->item.type == ARRIVAL)
        cout << ARRIVAL << cur->item.beginTime << ":" << cur->item.transactionLength << " ";
    else if (cur->item.type == DEPARTURE)
        cout << DEPARTURE << cur->item.beginTime << " ";
}
cout << endl;

}

The problem with this is that the output I want is for it to display the A or the D. Not the integer values associated with the letters. How exactly do I do this?

Event List: 652:5 686

I want that to read

Event List: A2:5 D6

Upvotes: 1

Views: 91

Answers (1)

It's very simple: cast it to a char:

cout << char(ARRIVAL) << cur->item.beginTime << ":" << cur->item.transactionLength << " ";

The same for DEPARTURE.

The reason why it works this way is that when used in an expression, enumerations are normally promoted to int (or a larger integral type if their values don't fit into an int). You have the correct char value inside the enum, however, so you can just cast it.

Upvotes: 2

Related Questions