By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 1,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

Urgent help in ENUM

P: 1
I am still new to C++ programming and I have a project that is going to due pretty soon. Here is my question.

Does anyone knows how to check through all the members in enum? For example the program below.

enum test{
FRUITS_APPLE= 5;
FRUITS_ORANGE= 19;
FRUITS_PEAR= 7;
FRUITS_end
};


for(int i=0; i<FRUITS_end; i++)
{
if( j==FRUITS_end[i])
{
printf("Pass");
}
}


Can I know if I am doing correctly? Thanks for the help.
Jun 15 '06 #1
Share this Question
Share on Google+
2 Replies


Banfa
Expert Mod 5K+
P: 8,916
That wont work an enum is not an array and in C++ an enum is not an integer, I'd be surprised if that compiled. You need something more like

Expand|Select|Wrap|Line Numbers
  1. enum test{
  2. FRUITS_APPLE= 5,
  3. FRUITS_ORANGE= 19,
  4. FRUITS_PEAR= 7,
  5. FRUITS_end
  6. };
  7.  
  8. switch(j)
  9. {
  10. case FRUITS_APPLE:
  11. case FRUITS_ORANGE:
  12. case FRUITS_PEAR:
  13.     printf("Pass");
  14.     break;
  15.  
  16. default:
  17.     break;
  18. }
  19.  
or

Expand|Select|Wrap|Line Numbers
  1. enum test{
  2. FRUITS_APPLE= 5,
  3. FRUITS_ORANGE= 19,
  4. FRUITS_PEAR= 7,
  5. FRUITS_end
  6. };
  7.  
  8. static enum test testValues[] = { FRUITS_APPLE, FRUITS_ORANGE, FRUITS_PEAR };
  9.  
  10. for(int i=0; i<(sizeof testValues/sizeof testValues[0]); i++)
  11. {
  12.     if( j==testValues[i])
  13.     {
  14.         printf("Pass");
  15.     }
  16. }
  17.  
Of course in C++ this is dependent on j being of type test.
Jun 15 '06 #2

Banfa
Expert Mod 5K+
P: 8,916
enum test{
FRUITS_APPLE= 5;
FRUITS_ORANGE= 19;
FRUITS_PEAR= 7;
FRUITS_end
};
BTW this is a really bad way to define an enum (and not just because you've used ; where you should have , because if I add extra members like so

Expand|Select|Wrap|Line Numbers
  1. enum test{
  2.     FRUITS_APPLE= 5,
  3.     FRUITS_ORANGE= 19,
  4.     FRUITS_PEAR= 7,
  5.     FRUITS_BANANA,
  6.     FRUITS_PLUM,
  7.     FRUITS_STRAWBERRY,
  8.     FRUITS_RASPBERRY
  9.     FRUITS_BLUEBERRY,
  10.     FRUITS_APRICOT,
  11.     FRUITS_PEACH,
  12.     FRUITS_NECTERINE,
  13.     FRUITS_KIWI,
  14.     FRUITS_CHERRY,
  15.     FRUITS_GRAPE,
  16.     FRUITS_PINEAPPLE
  17.     FRUITS_end
  18. };
Then FRUITS_ORANGE and FRUITS_PINEAPPLE have the same value (and I have seen exactly this problem hold up a software release for a week while it was ironed out). If you must give the enum members specific values make sure they are in numerical order

Expand|Select|Wrap|Line Numbers
  1. enum test{
  2.     FRUITS_APPLE= 5,
  3.     FRUITS_PEAR= 7,
  4.     FRUITS_ORANGE= 19,
  5.     FRUITS_end
  6. };
  7.  
that way you are less like to run into problems when people add members to the end. However even declared like this there are problems because FRUITS_end will have the value 20 which is meaningless. If you let the enum auto assign values like so

Expand|Select|Wrap|Line Numbers
  1. enum test{
  2.     FRUITS_APPLE,
  3.     FRUITS_PEAR,
  4.     FRUITS_ORANGE,
  5.     FRUITS_end
  6. };
  7.  
The it will start at 0, every item will have a unique value and FRUITS_end will have the value 3 which is meaningful it is the number of FRUITS (although is is only really useable in C as in C++ enum is not an integer.
Jun 15 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.