Thread: Question about enum.

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    16

    Question about enum.

    Hello,

    i did not have used enum so far, so i have the following questions:

    Code:
    enum Numbers { ONE, TWO, THREE, FOUR, FIVE };
    typedef enum { ONE, TWO, THREE, FOUR, FIVE } Numbers;
    1. What is the difference?

    2. I have the words ONE, TWO..., why do i need the word Numbers?

    3. How i have to use Numbers, i tried some ways but nothing worked?

    Thank you!

    greetings
    newatC

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by mewatC
    Hello,

    i did not have used enum so far, so i have the following questions:

    Code:
    enum Numbers { ONE, TWO, THREE, FOUR, FIVE };
    typedef enum { ONE, TWO, THREE, FOUR, FIVE } Numbers;
    1. What is the difference?

    2. I have the words ONE, TWO..., why do i need the word Numbers?

    3. How i have to use Numbers, i tried some ways but nothing worked?

    Thank you!

    greetings
    newatC
    1. usage to declare the new var
    enum Numbers myvar;
    or just
    Numbers myvar;
    2.to be possible to declare var of this type, so the compiler can check the incorrect type handling
    For example one function awaits as a parameter var of type Numbers, and another one Colors. If you store both Numbers and Colors in the int var - compiler cannot check for you, if the by mistake pass the color insted of the number.
    But if you declare to different enums and use them for declaring functions parameters - compiler can find the typing mistakes for you. So give the compiler to make as much work as he can in checking your code.
    3.
    Code:
    Numbers choice;
    "Enter number:"
    read string
    if(strcmp(string,"One")==0) choice = ONE;
    else if(strcmp(string,"Two")==0) choice = TWO;
    ...
    
    some other places
    switch (choice)
    {
    case ONE:...
    case TWO: ...
    ...
    }
    You don't need to stroe strings - only small var
    You can use switch on it
    You can make printing strings depending on choice language dependend (input can be in english, but output in any other language) etc
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Quote Originally Posted by mewatC
    Hello,

    i did not have used enum so far, so i have the following questions:

    Code:
    enum Numbers { ONE, TWO, THREE, FOUR, FIVE };
    typedef enum { ONE, TWO, THREE, FOUR, FIVE } Numbers;
    1. What is the difference?
    I'll answer the first question:

    There's three possibilities in declaring enums (same with structs)

    1. enum Numbers { ONE, TWO, THREE, FOUR, FIVE };
    2. typedef enum { ONE, TWO, THREE, FOUR, FIVE } Numbers;
    3. typedef enum Numbers { ONE, TWO, THREE, FOUR, FIVE } Numbers;

    With the first case you can only declare Numbers as:
    Code:
    enum Numbers num = ONE;
    Doing Numbers num = ONE; will give a compiler error

    With the second case you can only declare Numbers as:
    Code:
    Numbers num = ONE;
    Doing enum Numbers num = ONE; will give an error

    With the third case you can do both:

    Code:
    enum Numbers num = ONE;
    Numbers num = ONE;

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    16
    Hello,

    thank you for your answers.

    greetings,
    newatC

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  2. Question on Enum
    By markcls in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 08:19 AM
  3. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  4. Enum question
    By Bill 101 in forum C Programming
    Replies: 4
    Last Post: 10-31-2002, 12:33 AM
  5. enum question
    By incognito in forum C++ Programming
    Replies: 1
    Last Post: 12-30-2001, 12:04 AM