A program to illustrate enumerations. Program is as follows:
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 |
#include <iostream> using namespace std; int main() { enum direction{left='l', right='r'}; char ch; cout<<"Enter direction (as character): "; cin>>ch; direction d = (direction) ch; switch(d) { case left: { cout<<"Go left"; break; } case right: { cout<<"Go right"; break; } default: { cout<<"Wrong direction"; } } return 0; } |
Input and output for the above program are as follows:
1 2 |
Enter direction (as character): r Go right |