Thread: about enum ?

  1. #1
    Unregistered
    Guest

    Angry about enum ?

    is there anyway i can enum a number range ?

    eg
    instead of enum { black=1, white =2}

    i want black to have a range 1~10 ?

    i tried enum{black=1-10) , it compiles , but doesn't work

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: about enum ?

    Originally posted by Unregistered
    is there anyway i can enum a number range ?

    eg
    instead of enum { black=1, white =2}

    i want black to have a range 1~10 ?

    i tried enum{black=1-10) , it compiles , but doesn't work
    It does work. Just not the way you want. It equates to: "black = -9".

    No, there is no way to use ranges. You cannot do this with case statements or enums. Your best bet is:

    enum { black=1, white=11, red=21 ... };

    if( color < white ) printf("black"); else
    if( color < red ) printf("white"); else
    if( ... ) ...

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I wrote this to help explain/expand the answer, but I see quzah has already done something similar. Here it is anyway:
    Code:
    #include <stdio.h>
    
    enum Sizes {MICROSCOPIC, TINY, SMALL, MEDIUM, BIG, EVENBIGGER, BLOODYHUGE};
    
    int main(void)
    {
    	enum Sizes MySize = BIG;
    	
    	printf ("My Size is %d\n", MySize);
    	
    	if (MySize > MEDIUM)
    		printf ("That's big enough!\n");
    	else
    		printf ("That's too small!\n");
    		
    	return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic question on enum
    By MasterM in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2009, 09:16 PM
  2. enum switchcase and windows message q
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 11-27-2006, 01:16 PM
  3. Conflicting enum issue
    By 7force in forum C Programming
    Replies: 1
    Last Post: 07-05-2006, 03:51 AM
  4. Switch case and enum help
    By SomeCrazyGuy in forum C++ Programming
    Replies: 9
    Last Post: 04-21-2005, 08:53 PM
  5. enum
    By JerryL in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2004, 05:45 PM