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.
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 -
enum test{
-
FRUITS_APPLE= 5,
-
FRUITS_ORANGE= 19,
-
FRUITS_PEAR= 7,
-
FRUITS_end
-
};
-
-
switch(j)
-
{
-
case FRUITS_APPLE:
-
case FRUITS_ORANGE:
-
case FRUITS_PEAR:
-
printf("Pass");
-
break;
-
-
default:
-
break;
-
}
-
or -
enum test{
-
FRUITS_APPLE= 5,
-
FRUITS_ORANGE= 19,
-
FRUITS_PEAR= 7,
-
FRUITS_end
-
};
-
-
static enum test testValues[] = { FRUITS_APPLE, FRUITS_ORANGE, FRUITS_PEAR };
-
-
for(int i=0; i<(sizeof testValues/sizeof testValues[0]); i++)
-
{
-
if( j==testValues[i])
-
{
-
printf("Pass");
-
}
-
}
-
Of course in C++ this is dependent on j being of type test.
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 - enum test{
-
FRUITS_APPLE= 5,
-
FRUITS_ORANGE= 19,
-
FRUITS_PEAR= 7,
-
FRUITS_BANANA,
-
FRUITS_PLUM,
-
FRUITS_STRAWBERRY,
-
FRUITS_RASPBERRY
-
FRUITS_BLUEBERRY,
-
FRUITS_APRICOT,
-
FRUITS_PEACH,
-
FRUITS_NECTERINE,
-
FRUITS_KIWI,
-
FRUITS_CHERRY,
-
FRUITS_GRAPE,
-
FRUITS_PINEAPPLE
-
FRUITS_end
-
};
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 -
enum test{
-
FRUITS_APPLE= 5,
-
FRUITS_PEAR= 7,
-
FRUITS_ORANGE= 19,
-
FRUITS_end
-
};
-
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 -
enum test{
-
FRUITS_APPLE,
-
FRUITS_PEAR,
-
FRUITS_ORANGE,
-
FRUITS_end
-
};
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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
{
|
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;
|
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
|
by: zoltan |
last post by:
Hi,
Consider a structure as follows :
struct dummy
{
int a;
int b;
int c;
};
| |
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>
|
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?
|
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),
|
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,...
|
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...
|
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,...
| |
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |