hello;

can we say this when defining an enumeration :

enum letter{A=4,B=3,C,D,E};

i mean give letter B value smaller than the value of the letter A , is it possible ?


and what is the real benifet of the enumeartion in general ?

sometimes when making "cout" statement , the value appears as integers , is there any way that allow as see the real value of it , i mean when saying :
letter my;
my=C;
cout<<C;

i want the output here to be C not 4

>i mean give letter B value smaller than the value of the letter A , is it possible ?
Yes.

>and what is the real benifet of the enumeartion in general ?
Consider this:

enum animal {CAT, DOG, FISH, COW};
animal a = FISH;

if( a == FISH )
{
    cout << "The animal is a fish..." << endl;
}

See the benefit?

>sometimes when making "cout" statement , the value appears as integers , is there any way that allow as see the real value of it , i mean when saying...
>i want the output here to be C not 4

Can you give a full example (keep the code as short and sweet as possible), which demonstrates this?

Hope this helps!

see this code :

enum letter{A=4,B=3,C,D,E};

letter my;
my=C;
cout<<"The value of the variable C is "<<C;

the output here will be :
The value of the variable C is 4 ,
isn't it !

how could we make the output as :
The value of the variable C is C

how could we make the output as :
The value of the variable C is C

I think by using switch block

If you are forced to use enum:

cout<<"the value of variable C is";

switch(my){
case A:
    cout<<"A";
    break;
case B:
    cout<<"B";
    break;
case C:
    cout<<"C";
    break;
case D:
    cout<<"D";
    break;
case E:
    cout<<"E";
    break;
}

I hope that was useful.:)

thanks 4 all of U ,
examples are clear ,
and i get the point i want

hi again;

i was searching in some powerpoint files talking about enumeration and found this sentence :

enum typeName{ value1, value2, value3, ....};

where :
value1 < value2 < value3 <...

what does this mean ! we can not give "value1" a value more than "value2" ?

i try to make this in C++ program and there is no errors or any problem , but am not sure if this possible or not !

>what does this mean ! we can not give "value1" a value more than "value2" ?
You even didn't take a look at the link in siddhant3s' post, what a shame!

>what does this mean ! we can not give "value1" a value more than "value2" ?
You even didn't take a look at the link in siddhant3s' post, what a shame!

No I do , and all what i found was :

If you don't specify values for enum constants, the values start at zero and increase by one with each move down the list.

So ! what is the answer now , could we give the previous variable a valuue more than the next one or not !

If you specify a value, it overrides the rule. If you don't specify a value, it defaults to one more than the previous constant. If you don't specify any value for the first constant, it starts at 0:

enum 
{ 
    A,    // A == 0
    B,    // B == 1
    C=10, // C == 10
    D=20, // D == 20
    E,    // E == 21
    F     // F == 22
};

If you specify a value, it overrides the rule. If you don't specify a value, it defaults to one more than the previous constant. If you don't specify any value for the first constant, it starts at 0:

enum 
{ 
    A,    // A == 0
    B,    // B == 1
    C=10, // C == 10
    D=20, // D == 20
    E,    // E == 21
    F     // F == 22
};

So , it is possible to writ this :

enum 
{ 
    A,    // A == 0
    B,    // B == 1
    C=100, // C == 100
    D=20, // D == 20
    E,    // E == 21
    F     // F == 22
};

So , it is possible to writ this :

enum 
{ 
    A,    // A == 0
    B,    // B == 1
    C=100, // C == 100
    D=20, // D == 20
    E,    // E == 21
    F     // F == 22
};

Well, what has Tom Gunn just said?
Apply that to your example and you'll know :)

Ok , thank U
i got it now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.