and where is enumeration with name is benefficial.Code:enun{go, stop, ready, halt} vs enum status{go, stop, ready, halt};
any help would be appriciable
and where is enumeration with name is benefficial.Code:enun{go, stop, ready, halt} vs enum status{go, stop, ready, halt};
any help would be appriciable
Last edited by Username changed; 06-14-2014 at 09:09 AM.
I suggest that you start thinking instead of just asking questions. Like, why would you use an enumeration in the first place?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
that i know obviosly.........for declaring symbolic integer constant.....
but i don't see any idea behind making a type for enum
Write a function that, given the status value as an argument, prints the corresponding status name to standard output.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
i tried but getting its constant value rather than its name.......it would be plesure if u say..how to do itCode:#include<iostream> using namespace std; enum status{go,stop,be_ready}; int show_status(status st) { switch (st) { case 0:cout<<(status)0;break; case 1:cout<<(status)1;break; case 2:cout<<(status)2;break; default: cout<<"unknown"; } return 0; } int main() { int s; status st; cout<<"Enter Status "; cin>>s; st=(status)s; show_status(st); return 0; }
I was expecting something like this:Originally Posted by Rohit Kumar
Since you have named constants, it is ridiculous to still be using 0, 1 and 2 in the switch. But, if you want to get input from the user and store it in a variable of type status, then you need to do some checking of the input, not merely cast it (and it might not even be an integer in the first place).Code:enum status {go, stop, ready, halt}; void print_status(status status_value) { using std::cout; switch (status_value) { case go: cout << "go"; break; case stop: cout << "stop"; break; case ready: cout << "ready"; break; case halt: cout << "halt"; break; } }
That said, read up on enum classes in C++11.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
hey but i thought u asked it to do without using string constant in cout...like "go" "stop" "ready" halt"......i thought there would be some way to directly output the name of enumerators........but ok thanks....for helping so much m8..
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?