Thread: difference between a enumeration with name and without name......

  1. #1
    Deleted Account
    Join Date
    Oct 2013
    Posts
    39

    difference between a enumeration with name and without name......

    Code:
    enun{go, stop, ready, halt}
    vs
    enum status{go, stop, ready, halt};
    and where is enumeration with name is benefficial.


    any help would be appriciable
    Last edited by Username changed; 06-14-2014 at 09:09 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you start thinking instead of just asking questions. Like, why would you use an enumeration in the first place?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Deleted Account
    Join Date
    Oct 2013
    Posts
    39
    that i know obviosly.........for declaring symbolic integer constant.....
    but i don't see any idea behind making a type for enum

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Write a function that, given the status value as an argument, prints the corresponding status name to standard output.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Deleted Account
    Join Date
    Oct 2013
    Posts
    39
    Code:
    #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 tried but getting its constant value rather than its name.......it would be plesure if u say..how to do it

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Rohit Kumar
    i tried but getting its constant value rather than its name.......it would be plesure if u say..how to do it
    I was expecting something like this:
    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;
        }
    }
    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).

    That said, read up on enum classes in C++11.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Deleted Account
    Join Date
    Oct 2013
    Posts
    39
    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..

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Rohit Kumar View Post
    hey but i thought u asked it to do without using string constant in cout...like "go" "stop" "ready" halt"
    C++ has no built-in method for doing this. If you want to get string values from enumerations, you have to write the conversion yourself. I suspect that there are tools available to automate this process for you, but the code still has to be explicitly written.
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is enumeration
    By ncode in forum C Programming
    Replies: 3
    Last Post: 10-13-2011, 06:48 AM
  2. Enumeration
    By webren in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2005, 02:47 PM
  3. enumeration
    By C-Struggler in forum C Programming
    Replies: 5
    Last Post: 03-13-2003, 09:36 AM
  4. enumeration
    By lockpatrick in forum C Programming
    Replies: 2
    Last Post: 04-03-2002, 02:55 PM
  5. Enumeration in C and C++
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-27-2001, 01:12 PM