473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Urgent help in ENUM

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
2 2148
Banfa
9,065 Expert Mod 8TB
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
9,065 Expert Mod 8TB
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

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

Similar topics

3
by: Master | last post by:
Hi all, Please have a look at the following code snippet. int a; a=10; a=30; What i require here is a sniffer program or any kind of stuff which keeps on watching the memory allocated for...
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
0
by: sanjana | last post by:
hi i want to write a C# .net code to display a balloon in the task bar i have used notifyicon class to get the icon in the task bar then i have used Shell_NotifyIcon and NotifyIconData structure...
14
by: zoltan | last post by:
Hi, Consider a structure as follows : struct dummy { int a; int b; int c; };
1
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
0
by: nassim.bouayad.agha | last post by:
Hello, here is my table declaration : CREATE TABLE staff_member( pay_id varchar(64) NOT NULL default '', manager_pay_id varchar(64), location_name varchar(64), first_name varchar(64) NOT NULL...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.