Understanding Enumerations

This is a discussion on Understanding Enumerations within the C++ Programming forums, part of the General Programming Boards category; Having had trouble understanding enumerations, i decided to have a brief trial and error session with my compiler to observe ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    27

    Understanding Enumerations

    Having had trouble understanding enumerations, i decided to have a brief trial and error session with my compiler to observe how they work.

    Code:
    enum colors {red,yellow,green,brown,blue,pink,black} mycolor;
    From this i continued to conclude that mycolor could have assigned any of the defined constants but would not compile using a constant that was not defined (logically). ie:

    Code:
    mycolor = red;
    Is ok.

    Code:
    mycolor = orange;
    Errors.

    I also assume that you cannot assign a value to an enumeration.

    What i do not understand is the purpose of enums. I could see then serving a similar purpose to a switch statement or perhaps an if conditional however i do not understand how you would use them in this fashion. I would really appreciate some guidance on this issue, or perhaps simply an example of how an enum would be used in a program.

    Thank you

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    >I also assume that you cannot assign a value to an enumeration.

    That is incorrect. You can assign integer values to them like so:
    Code:
    enum colors {red=10,yellow,green,brown,blue=44,pink,black} mycolor;
    In that example,
    red == 10
    yellow == 11
    green == 12
    brown == 13
    blue == 44
    pink == 45
    black == 46

    Enumerated values are great for switches, because they are based on the atomic type int. Switch statements can only be made on integers and chars. Using enums, however, you can switch a color or a day of the week or whatever because enums are really integers under the hood. The nice thing, however, is that you can give them meaningful names rather than constantly trying to remember what numbers correspond with what colors in your program.
    Last edited by joshdick; 04-18-2005 at 09:54 AM.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    Thank you for the information.

    >> I also assume that you cannot assign a value to an enumeration.

    Sorry, i phrased that badly, i was thinking along the lines of mycolor = "value"; But i guess doing so would not make much sense anyway.

    Code:
     enum colors {red,yellow,green,brown,blue,pink,black} mycolor;
     mycolor = green;
    
     switch (mycolor) {
      case red:
       cout << "the color is red";
       break;
      case yellow:
       cout << "the color is yellow";
       break;
      case green:
       cout << "the color is green (etc)";
       break;
     }
    Referring to the above, is that the general way you would use a switch statement with enumerations? Or is there a more effective approach.

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    That code looks about right to me. I'm not a compiler however
    Try the code. That's a good way to see whether something like that works.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    I just posted the following in another thread, and maybe it will help you:

    I want to simply type in the information and have it recorded into the appopriate value
    Enums aren't variables, they are constants, so you can't read input into an enum. Enums just make your code more readable, they don't actually do anything logic wise. In fact, I suggest you forget about them completely since they seem to be a bright light blinding you from seeing what you need to do. After you work out the logic of your program, and finish writing it so that it works correctly, then if you have time, and you want to make your code more readable, you can go back and add some enums where appropriate. At this point in your C++ learning, don't dwell on enums--they are syntactic sugar.

    For instance, this might be your main() file:
    Code:
    #include <iostream>
    #include "fish.h" 
    
    using namespace std;
    
    void display_message(int fishType);
    
    int main()
    {
    	int input;
    	cout<<"Enter an int designating the the type of fish you caught\n";
    	cout<<"(bass=0, trout=1, carp=2): ";
    	cin>>input;
    
    	display_message(input);
    	
    	return 0;
    }
    and here is "fish.h":
    Code:
    //fish.h
    
    #ifndef __FISH_H__
    #define __FISH_H__
    
    #include <iostream>
    using namespace std;
    
    void display_message(int fishType)
    {
    	switch (fishType)
    	{
    	case 0:
    		cout<<"Last year there was a blight on those fish and 4,000 were killed."<<endl;
    		break;
    	case 1:
    		cout<<"According to statistics, that is the most popular fish to eat."<<endl;
    		break;
    
    	case 2:
    		cout<<"In 2004, 40,000 anglers ranked that fish as one of the\n";
    		cout<<"best fighters pound for pound."<<endl;
    		break;
    	default:
    			cout<<"I've never caught any of those."<<endl;
    	}
    }
    
    #endif //__FISH_H__
    In the file "fish.h", it's not exactly clear what 0, 1, and 2 mean. In fact, as you are programming that, you may even forget what they mean and get the messages mixed up. So, you might create an enum:
    Code:
    //fish.h
    
    #ifndef __FISH_H__
    #define __FISH_H__
    
    #include <iostream>
    using namespace std;
    
    void display_message(int fishType)
    {
    	enum {bass, trout, carp};  //anonymous enum
    	
    	switch (fishType)
    	{
    	case bass:
    		cout<<"Last year there was a blight on those fish and 4,000 were killed."<<endl;
    		break;
    	case trout:
    		cout<<"According to statistics, that is the most popular fish to eat."<<endl;
    		break;
    
    	case carp:
    		cout<<"In 2004, 40,000 anglers ranked that fish as one of the\n";
    		cout<<"best fighters pound for pound."<<endl;
    		break;
    	default:
    			cout<<"I've never caught any of those."<<endl;
    	}
    }
    
    #endif //__FISH_H__
    Now, it's clearer what's going on in "fish.h", and it may be easier for you as a programmer to keep from getting the messages in the wrong cases. Note, that the first "fish.h" worked perfectly fine in the program--it just wasn't that readable--and the only thing adding an enum did was change some numbers to words.

    In your statement here:
    Code:
    enum colors {red,yellow,green,brown,blue,pink,black} mycolor;
    an integer value is assigned to each of those words. Like with an array, the values start at 0, and so red=0, yellow=1, green=2, etc. So, when the compiler comes across the word red in your program, it replaces the word red with 0, and when it sees the word yellow, it replaces the word yellow with 1, etc.
    Last edited by 7stud; 04-18-2005 at 04:45 PM.

  6. #6
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    A member of the boards, Kermit, wrote a very good article on enumeration here:
    http://home.golden.net/~uberkermit/c...ing/enum.shtml
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Understanding Headers
    By AeonMoth in forum C++ Programming
    Replies: 2
    Last Post: 06-27-2007, 06:53 AM
  3. trouble understanding the source file structure
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 05-26-2006, 07:46 PM
  4. understanding recursive functions
    By houler in forum C Programming
    Replies: 7
    Last Post: 12-09-2004, 12:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21