enumerations

i want to use enumerations and want to show the exact enumerators as output instead of their integer values.

#include<iostream.h>
#include<conio.h>
enum etype{manager,accountant,director};
main()
{
etype e1;
char a;
cout<<"Enter 1st letter of employee type(m,a,d): ";
cin>>a;
switch(a)
{
case 'm':e1=manager;break;
case 'a':e1=accountant;break;
case 'd':e1=director;break;
}
cout<<e1;
getch();
}
here if user enters m,the output is 0,i-e integer value of enumerator,how can i make output as manager by entering m.
oh i got it like this.if there is some other way plz rep..

#include<iostream.h>
#include<conio.h>
enum etype{manager,accountant,director};
main()
{
etype e1;
char a;
cout<<"Enter 1st letter of employee type(m,a,d): ";
cin>>a;
switch(a)
{
case 'm':e1=manager;break;
case 'a':e1=accountant;break;
case 'd':e1=director;break;
}
cout<<e1;
getch();
}
Last edited on
This is almost the same but more convenient:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>

using namespace std;

enum etype{manager,accountant,director};
int main()
{
	etype e1;
	string a;
	cout<<"Enter type of employee (manager,accountant,director): ";
	cin>>a;
	if(a == "manager")
		e1 = manager;
	else if(a == "accountant")
		e1 = accountant;
	else if(a == "director")
		e1 = director;
	cout<<e1;
	return 0;
}
doesn't work..
Maybe your compiler is too old you can use #include<iostream.h> instead of #inlcude<iostream>
and add #include<string> and remove the using namespace std; this might make it work.
closed account (zb0S216C)
A few words of advice:
1) Enumerations are constant integers.
2) enums are somewhat structures. The members within an enum are integers. On line 13, 15, and 17, you assign e1 to a constant integer. Why?
3) On line 18, you print an instance of enum. Which member do you want to print? Note that enum instances maybe considered pointless. Also, enumerated values don't require the name of the instance to access it. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

enum ENUMERATIONS
{
    ENUM_ONE,
    ENUM_TWO
};

int main( )
{
    cout << ENUM_ONE << " " << ENUM_TWO << endl;
    cin.get( );
    return 0;
}

In this example, note that there are no instances of ENUMERATIONS, and yet I used ENUM_ONE, and ENUM_TWO.
@framework...that was helpful..thanks
Topic archived. No new replies allowed.