Thread: Can someone clearly explain to me what enums are.

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    30

    Can someone clearly explain to me what enums are.

    I started learning C++ no more than 4 days ago so please use the simplest vocabulary that you can manage while still being clear.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Surely your book will tell you this? What is it you're confused about?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    It's a method of representing numbers with words.

    Instead of manually maintaining that index 0 of your array represents a panda and index 1 represents a brown bear, you can use an enumeration and call elements in your array with array[panda_bear] = ...

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MutantJohn View Post
    It's a method of representing numbers with words.
    No it's not. An enum creates a type whose variables can only contain the specific values as represented within the enum. The values are strongly-typed named constants.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2015
    Posts
    30
    Quote Originally Posted by Elysia View Post
    Surely your book will tell you this? What is it you're confused about?
    Here is the specific example of what I'm talking about. I am supposed to be making a tic tac toe gmae using enums. Do I use enums for the spaces of the tic tac toe board or the x's, o's, and blank spaces. If you could explain why that would be awesome. On top of that, Im not sure how to use enums. When using them as input, do I type in "red" or "0". Also, what do enums represent? Functions, variables? I'm really confused about this.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What are the values each space is supposed to be able to hold in tac-tac-toe? It's X, O or Blank, right? We want each square to only hold these allowable values, and nothing else. That's where enums come in handy. They allow us to create a new type, and in that type, only specific allowable named constants are allowed to be assigned. Nothing else.

    Ex:
    Code:
    enum class MyEnum
    {
        Value1,
        Value2,
        Value3
    };
    
    MyEnum MyVar; // Create a variable of our enum type
    MyVar = MyEnum::Value1; // OK
    MyVar = 0; // Not OK: 0 is an int, not of type MyEnum
    MyVar = '0'; // Not OK: '0' is a char, not of type MyEnum
    To give you something to get started with.
    And to remind you, MyVar can only hold the values Value1, Value2, Value3 from MyEnum and nothing else. Trying to assign anything else will give you a compile error.
    Of course, Value1, ..., can be named anything you want. So can the enum itself.
    You can also compare to enum values:

    Code:
    if (MyVar == MyEnum::Value2) // Do something
    
    if (MyVar == 0.0) // Error: can't compare a double to MyEnum.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jan 2015
    Posts
    30
    Quote Originally Posted by Elysia View Post
    What are the values each space is supposed to be able to hold in tac-tac-toe? It's X, O or Blank, right? We want each square to only hold these allowable values, and nothing else. That's where enums come in handy. They allow us to create a new type, and in that type, only specific allowable named constants are allowed to be assigned. Nothing else.

    Ex:
    Code:
    enum class MyEnum
    {
        Value1,
        Value2,
        Value3
    };
    
    MyEnum MyVar; // Create a variable of our enum type
    MyVar = MyEnum::Value1; // OK
    MyVar = 0; // Not OK: 0 is an int, not of type MyEnum
    MyVar = '0'; // Not OK: '0' is a char, not of type MyEnum
    To give you something to get started with.
    And to remind you, MyVar can only hold the values Value1, Value2, Value3 from MyEnum and nothing else. Trying to assign anything else will give you a compile error.
    Of course, Value1, ..., can be named anything you want. So can the enum itself.
    You can also compare to enum values:

    Code:
    if (MyVar == MyEnum::Value2) // Do something
    
    if (MyVar == 0.0) // Error: can't compare a double to MyEnum.
    This has definitely helped, but I still have a few questions. Just to make sure I am not mistaken, MyVar is only a variable made to represent Value1, then when MyEnum MyVar is entered, it takes Value1 and uses that as input. If thats the case, couldn't you set Value1=MyVar within in the enum? Also, does this mean that enum is technically just a variable with a bunch of constants? Furthermore, aren't constants within the enum set to 0,1,2,... respectively by default? If so, could I just write "MyEnum 0;" instead? What does the "::" in your sample mean?

    Here's the example given for enums in the book.
    Code:
    enum RainbowColor {
    RC_RED, RC_ORANGE, RC_YELLOW, RC_GREEN, RC_BLUE, RC_INDIGO, RC_VIOLET
    };
    
    RainbowColor chosen_color = RC_RED;
    
    switch (chosen_color)
    {
    case RC_RED: /* paint screen red */
    case RC_ORANGE: /* paint screen orange */
    case RC_YELLOW: /* paint screen yellow */
    case RC_GREEN: /* paint screen green */
    case RC_BLUE: /* paint screen blue */
    case RC_INDIGO: /* paint screen indigo */
    case RC_VIOLET: /* paint screen violet */
    default: /* handle unexpected types */
    }
    Doesn't this only allow for "RainbowColor chosen_color" to equal "RC_RED"? Wouldn't that mean that I could just set "RC_RED" into "switch(chosen_color)"?

    Last request:
    This is a lot to ask especially since I'm already asking so much, but could you show me an example of how this can be implemented into a tic tac toe program? It still doesn't make too much sense to me in that regard. If it's too much to ask I understand. And thank you very very much in advance, I can't even explain how much of a help you are.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Aenn View Post
    This has definitely helped, but I still have a few questions. Just to make sure I am not mistaken, MyVar is only a variable made to represent Value1, then when MyEnum MyVar is entered, it takes Value1 and uses that as input...
    No. MyEnum is a type and MyVar is an variable of that type.
    This is no different from using an int. Consider:

    int MyVar1; // Myvar1 is a variable of type int
    MyEnum Myvar2; // Myvar2 is a variable of type MyEnum

    The only difference is that MyVar1 can take any value from min(int) to max(int), while MyVar2 can only takes the values listed in the enum, i.e. MyEnum::Value1, MyEnum::Value2, MyEnum::Value3.

    If thats the case, couldn't you set Value1=MyVar within in the enum?
    An enum is a type, not a variable, so that makes no sense. An enum does not take up memory and thus cannot hold any data.

    Also, does this mean that enum is technically just a variable with a bunch of constants?
    No, it's a type with a bunch of named constants.

    Furthermore, aren't constants within the enum set to 0,1,2,... respectively by default?
    They do have a numeric value so that they can be represented by computers. But normally we don't really mind that. That's an implementation detail. We're after the name of the constants instead of using 0, 1, 2, etc directly.

    If so, could I just write "MyEnum 0;" instead?
    No. MyEnum is not a variable. It makes no sense to type "Type Value". Only a variable can store a value; a type cannot.

    What does the "::" in your sample mean?
    It's a namespace, e.g. std::string, where "std::" refers to the std namespace. When creating an (strongly typed) enum, it creates a namespace wherein the named constants lie, as to not pollute the global namespace.

    Here's the example given for enums in the book.
    The book is stupid. I recommend you ignore it.

    Doesn't this only allow for "RainbowColor chosen_color" to equal "RC_RED"? Wouldn't that mean that I could just set "RC_RED" into "switch(chosen_color)"?
    Don't know what this means exactly.

    This is a lot to ask especially since I'm already asking so much, but could you show me an example of how this can be implemented into a tic tac toe program?
    No. I'm leaving that as an exercise on purpose. Start by getting down a simpler example first, then move on to tic-tac-toe.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-04-2014, 11:40 AM
  2. enums this time
    By sahilpatel_13 in forum C Programming
    Replies: 3
    Last Post: 03-11-2013, 12:14 PM
  3. Tricking enums
    By Ganoosh in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2006, 02:46 PM
  4. printing enums
    By xddxogm3 in forum C++ Programming
    Replies: 19
    Last Post: 08-09-2005, 12:08 PM
  5. enums!
    By newjamie in forum C Programming
    Replies: 1
    Last Post: 03-13-2002, 12:25 PM