473,661 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Urgent help in ENUM

1 New Member
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 2154
Banfa
9,065 Recognized Expert Moderator Expert
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 Recognized Expert Moderator Expert
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_PINEAPPL E 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
1148
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 the variable 'a'. As soon
21
4586
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
3587
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(); Control c = f; An enum value inherits from int but it doesn't get implicitly converted: HorizontalAlignment h = HorizontalAlignment.Center;
0
2552
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 for getting the balloon So now i have an icon n a balloon in the task bar But i wan tht when i click on the yellow windows balloon an event should be fired I know tht i have to use NIN_BALLOONUSERCLICK of Shell_NotifyIcon for
14
8644
by: zoltan | last post by:
Hi, Consider a structure as follows : struct dummy { int a; int b; int c; };
1
2172
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 <string>
34
11177
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 snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
0
2132
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 default '', last_name varchar(64) NOT NULL default '', e_mail varchar(64) NOT NULL default '', primary key (pay_id),
0
8343
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8758
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7364
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1986
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.