How canif you can seesee int...

We're offering banner ads on our site from just $5!
1. . 2. . 3. .
C/C++: How to convert int to enum?
Started by , Jul 06
Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
You cannot reply to this topic
16 replies to this topic
&&Members &&-&&Reputation:
Is there a way to convert an int to enum?
For example, I declared the following enum type:
wednesday,
Is there a way to set today to Wednesday using an integer, like this:
According to C documents, the following is legal, but result is not defined
today = (Days)0;
&&Members &&-&&Reputation:
Quote:Original post by faculaganymedeAccording to C documents, the following is legal, but result is not definedtoday = (Days)0;My C++ compiler has no problem with that (VS 2003). I've seen casting arbitrary integers to enums like that before, I think that's how you're supposed to do it.EDIT: Is this C or C++? There might be a difference between the two.EDIT2: Yeah, that's the right way in C++ at least. See these links:
&&Members &&-&&Reputation:
To be safe, define the first element zero.saturday = 0,...Kuphryn
Anonymous Poster_Anonymous Poster_*
&&Guests &&-&&Reputation:
You can do that just fine, without casting.
Another good practice is to have a last enumeration.
For example:enum Days
wednesday,
}DThen you can loop through your enum...for(int i = 0; i & LAST_DAY; i++)
if(today == wednesday)
std::cout && "Today is Hump-Day!!!";
&&Members &&-&&Reputation:
Thanks everyone for your quick feedbacks.I am programming in C++ using MS VS 6.0.Here's MSDN library doc that I was referring to ealier.
The very last line seems to say enum typecast is not possible.---------------------------------------------------------------------------enumenum [tag] {enum-list} [declarator];
// for definition of enumerated type
// for declaration of variable of type tagThe enum keyword specifies an enumerated type.An enumerated type is a user-defined type consisting of a set of named constants called enumerators. By default, the first enumerator has a value of 0, and each successive enumerator is one larger than the value of the previous one, unless you explicitly specify a value for a particular enumerator. Enumerators needn’t have unique values. The name of each enumerator is treated as a constant and must be unique within the scope where the enum is defined. An enumerator can be promoted to an integer value. However, converting an integer to an enumerator requires an explicit cast, and the results are not defined.In C, you can use the enum keyword and the tag to declare variables of the enumerated type. In C++, you can use the tag alone.In C++, enumerators defined within a class are accessible only to member functions of that class unless qualified with the class name (for example, class_name::enumerator). You can use the same syntax for explicit access to the type name (class_name::tag).For related information, see class and struct.Example// Example of the enum keywordenum Days
// Declare enum type Days{
// saturday = 0 by default
sunday = 0,
// sunday = 0 as well
// monday = 1
// tuesday = 2
wednesday,
// Variable today has type Days
// Error, redefinition of tuesdayenum D
// Legal in C and C++D
// Legal in C++ onlyyesterday =int i =
// L i = 2yesterday = 0;
// E no conversionyesterday = (Days)0;
// Legal, but results undefined
&&Members &&-&&Reputation:
Just having done a quick test, in VS.net 2003, this throws up no compiler or runtime errorsenum Days{ Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday,};int _tmain(){ Days normalDay, illegalD normalDay = (Days)2; illegalDay = (Days)234; return 0;}I'm assuming that the documentation is refering to the illegalDay line when describing the casting as undefined.Casting 2 (or any valid number) will give you the corresponding enum.
What 234 gives you I dont know.I really wouldn't worry about this to be honest, as long as you are using valid numbers to cast.
And as long as you have the max days enum mentioned in a earlier post, there should be no chance of you making an error (unless you declare the first value &0, in which case you should be careful)Spree
&&Members &&-&&Reputation:
Keep in mind that your assuming an enum is the size of an int.
It doesn't have to be:).
YOu can force it to be atleast as large as an in by assigning an enum entry to have a value greater than a short or word.An enum can be the size of a char or a int16 or an int 32 or an int64.So casting can be dangerous.CheersCHris
&&Crossbones+ &&-&&Reputation:
As others have mentioned, doing the cast like that should work everywhere as long as the int is legit.
Personally, I've moved away from enums due to other reasons [I tired of doing string-&enum enum-&string conversions...] and only really use them now when the result absolutely never, ever needs displayed or inputed by users or between components. Which is rare to say the least.
&&Members &&-&&Reputation:
Quote:Original post by kuphrynTo be safe, define the first element zero.saturday = 0,...KuphrynThe value of the first enum element is always 0 if it is not initialized (see the C++ standard, section 7.2, § 1).Regards,
-- Emmanuel D. [] []
&&Members &&-&&Reputation:
Quote:Is there a way to set today to Wednesday using an integer, like this:today = 3According to C documents, the following is legal, but result is not definedtoday = (Days)0;Why not just do this: today =an enumeration is just a list of constants so it would be just the same as doing this:#define saturday
0int today =orconst int saturday = 0;int today =Someone please correct me if i'm wrong.
&&Members &&-&&Reputation:
Quote:Original post by DigiDude
Why not just do this: today =
For my app, I couldn't do this because I don't know the day beforehand, this data has to be read from a file.
In my original plan, I was suppose to read the data in as a string from the file, e.g. "saturday", but I didn't know how to convert string to enum.
So I thought of using integers to represent days, to make things easier.
today ?= "saturday"
Quote:Original post by Telastyn
Personally, I've moved away from enums due to other reasons [I tired of doing string-&enum enum-&string conversions...] and only really use them now when the result absolutely never, ever needs displayed or inputed by users or between components. Which is rare to say the least.
How do you convert string to enum, and enum to string?
&&Members &&-&&Reputation:
Couldn't you read it in as integers then use a switch statment like:int blah = [whatever you read in from file];Dswitch (blah){case 0:
today =...}
&&Crossbones+ &&-&&Reputation:
Quote:Original post by faculaganymedeHow do you convert string to enum, and enum to string?Something like this:enum hextypes
{NOTHING,GRASSLAND,PLAINS,SPARCEFOREST,FOREST,SWAMP,HILL,MOUNTAIN,DESERT,TUNDRA,JUNGLE,SHALLOWS,OCEAN,HEXTYPECOUNT};
char *hextypesymbols[]
={"_","g","p","f","F","w","h","M","d","t","j","-","~"};char *hextypenames[]={"nothing","grassland","plains","sparceforest","forest","swamp","hill","mountain","desert","tundra","jungle","shallows","ocean"};intchar
ht=NOTHING;cout && hextypenames[FOREST];for (x=0;x&HEXTYPECOUNT;++x){
if (buf==strstr(buf,hextypenames[x])){
ht=(hextypes)x;
}}though using char * is asking for trouble. And editing a source file whenever you want to add a new enum is asking for trouble. And keeping 2-3 arrays in perfect sync is asking for trouble.So I don't do this any more. I made a templated class which stores a name/ID pair as well as arbitrary properties associated with them. It then can convert nicely between the std::string, the ID, and the properties. Like for the example above, the properties like how much food a hextype yields when farmed is now stored along with the name/ID, rather than a seperate table using the enum as a key. The bonus is that the ID's can be dynamically generated, so the name/ID/properties can be loaded from file or otherwise generated at run-time.Benefits all around.
&&Members &&-&&Reputation:
To be totally safe just don't make today type Days (plural type names feel so wrong), use a normal number typeenum Days
// Declare enum type Days{
// saturday = 0 by default
// sunday = 1
// monday = 2
// tuesday = 3
wednesday,
friday}You can still use the enum constants outside the type & they function exactly as their number counterpartsI often use unnamed enums just for storing constant values, the downside though is that enums can only be intergral(also unnamed types aren'])
_______________________________ ________ _____ ___ __ _`By offloading cognitive load to the computer, programmers are able to design more elegant systems' - Unununium OS regarding Python
&&Members &&-&&Reputation:
Quote:Original post by DigiDudeCouldn't you read it in as integers then use a switch statment like:int blah = [whatever you read in from file];Dswitch (blah){case 0:
today =...}Why would you want to replace what could be a single line of code with a huge switch statement?
Progress is born from the opportunity to make mistakes.My prize winning
looks one move ahead, can you beat it?
&&Members &&-&&Reputation:
Quote:Original post by DigiDudeCouldn't you read it in as integers then use a switch statment like:Thanks.
Enum Days is just a quick and dirty example.
In the actual app, the list may be very long, so using a switch statement requires a long list of cases.
But, it's definitely an idea worth considering.Quote:Original post by Telastynthough using char * is asking for trouble. And editing a source file whenever you want to add a new enum is asking for trouble. And keeping 2-3 arrays in perfect sync is asking for trouble.Yes, I agree.
This is very messy.
Thanks for sharing :)
Anonymous Poster_Anonymous Poster_*
&&Guests &&-&&Reputation:
Quote:Original post by faculaganymedeHi all,Is there a way to convert an int to enum?For example, I declared the following enum type:*** Source Snippet Removed ***Is there a way to set today to Wednesday using an integer, like this:today = 3This is legal in C so long as one of the enumerated constants has the value 3.Quote:According to C documents, the following is legal, but result is not definedtoday = (Days)0;This is legal in C so long as one of the enumerated constants has the value 0.C++ may require a cast, I'm not sure.
Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
Reply to quoted posts&&&&& &&&
Need an account?
Forum Password
Remember me
This is not recommended for shared computers
Sign in anonymously
Don't add me to the active users listThe Promo Skin is now available! Get , or see a .
Wanna have a successful and profitable business? Then you need leads… and lots of them.
Nothing happens without a lead. No sales, no revenue, no profit, no nothin’.
Below are 10 ways to generate MORE leads without spending a bundle.
But before we get to the list, there are a couple of lead generation ground rules you should be clear about. Without understanding these, you can end up wasting a lot of time and money.
Ground Rule #1: Build a Value Machine for the Long Term
WARNING: This is boring, far from sexy, and nothing even close to resembling a silver bullet lead generating solution. And THAT’S why it’s so effective
Don’t ignore it now or you run the risk of being sorry later.
We’ll start off with the lead generation technique that most people will not have the discipline or patience to pursue. That is creating a REAL value machine for your market. This could be a blog you post to daily, or a video series you create daily… or something else.
Sounds simple, but it’s far from easy. I dare you, try it and you’ll see. Write a 100% value blog post every day for a month. This is work from top to bottom. That’s why it’s so effective, in my opinion. Because the majority of your competition is most likely lazy. Use that to your advantage—.
Sometimes, however, you need leads yesterday so that you can put food on the table today. In that case, speed is what works, and we’ll get to that in a few moments. However, even if that’s the case, ALWAYS have a long term lead generation system running along side your short term campaigns.
Build your asset before you need it.
Ground Rule #2: Understand That the Money is Not in the List
It appears that one of my lists currently has little to no “money” hiding in it. That’s because I filled it with freebie seekers instead of potential buyers. I built a list before I had something to sell. If I had it to do over, I wouldn’t bother. Learn from my mistake. You build lists NOT to get leads, but to get buyers.
Unless you’re just lonely, you don’t want to be generating just any lead… you want to be generating leads that turn into buyers. It’s easy to build a list of people that just want free stuff. Those are not the kinds of leads you’re looking for.
Consider this: cast your lead generating net as wide as possible. But as quickly as you can, like on the “thank you” page, make it clear that you’re SELLING. You’ll totally scare away the folks who are only looking for a free lunch. As long as you provide value WHILE you sell, the true potential buyers will stick with you.
You are running a business, not a charity. So make that clear (unless of course you ARE running a charity), otherwise when you DO try to sell, it will be jarring because it will be totally inconsistent with what came before.
Ground Rule #3: You Don’t Have Time to Wait Around for a Relationship
People buy because they want things. They buy because they’re yearning for the perceived benefits of what you provide. Yes a relationship helps. But you can’t pay your bills or pay your expenses with relationships. So ask them to buy FAST.
Isn’t that why you’re in business? Don’t make excuses for it. Just sell.
OK, so let’s jump in to 10 ways to get you more leads:
1. Viral Video Works
Create a viral video and post it to Youtube(R), or better yet, to both Youtube(R) and your own site. (if it goes viral, trust me, you’ll be happy you put it on your own site once all the link love starts coming.)
On the video page, make an offer for the NEXT video in exchange for the optin.
Go buy some cheap PPC ads on Facebook(R), not promoting your offer, just promoting the video.
People (especially people on Facebook(R)) like to be entertained, so make sure the content of your video is clearly INFOTAINMENT. Don’t just teach… Teach, sell or pre-sell and entertain.
What you don’t know is if all these leads you’re attracting are going to be buyers. You won’t know that until you try it. (See item #3.) This is business, if you want guarantees, do something else.
2. Leverage Your Real Estate and Don’t Be Lazy
In my opinion, the jury is still out on just how profitable social media is for generating sales, but as for spreading ideas, it can be darn effective.
Give your leads the opportunity to spread your message for you. So on your “thank you page,” put a
which hardly stands out on someone’s Facebook feed). Add in a
and any other social gizmos that your market uses to spread things.
Better yet, if you know what you’re doing with code, make the “Post to Your Wall” pop up immediately. You can pre-fill it with a title, picture, link and message so people on Facebook(R) can spread it around with a SINGLE click.
You might pay for ONE lead, but that single lead might very well bring you 10 more.
3. Don’t Wait to “Butter ‘em Up” Just SELL Them
Once you’ve leveraged a new lead’s ability to spread your message for you (and hopefully generate more leads), give them a test. Put an offer (hard or soft) in front of them right away.
Again, you don’t want just ANY lead, you want ones that buy. The more of them that buy, the more leads you’ll be able to afford.
4. Don’t Ignore the Simple Stuff
Please make sure you don’t discount simple ways to generate leads that are cheap and or free. For example, do you have an offer in your email signature? If not, get one.
5. Free Junk is Getting Harder to Give Away
I’m sure in the old days, you could offer just about any “freemium” as an enticement for people to join your list. Just saying, “Sign-up for my newsletter” could get some results. But the bar is getting higher, so sending your “Top 5 Tips For XX” just doesn’t carry the same weight it used to.
People have too much junk on their hard drives already. So make the “Free” thing you give away something that actually has inherent value. Some suggestions: software your leads can USE, a definitive resource (101 tips for XX) that they will come back to again and again, something that solves a real point of pain for the people you are trying to attract.
In other words, to get more leads, you should be offering them something that is MORE and BETTER than anything they can get elsewhere.
Raise the bar or you will be left out.
Editor’s note from Derek Halpern: When we launched the Thesis blog, I wrote an in-depth ebook that taught people how to use nonverbal communication online to get leads and subscribers. It converted extremely well for us. The first day we had a 70% opt-in using .
6. People Want What They Cannot Have
If there’s one pillar of sales psychology, this is it: people want what they cannot have. So how can you build THAT into your lead generation campaigns? How can you add an element of scarcity?
Perhaps you could create a new “freemium” each and every month. Once the month is gone, that “freemium” disappears and gets turned into a product for sale.
Again, it’s work. But people who are willing to work hard have a built in competitive advantage.
If your funnel is always open to everyone, everywhere, what’s the reason they should contact you TODAY and not 4 years from now? It helps to have a good answer to that question.
7. Detach Yourself from Results and Think Bigger
I’d have to say that most of my lead generation campaigns stink. They don’t produce much. This is a numbers game. So figure out ways to use that fact to your advantage. Instead of doing a single lead generation campaign, why not launch 10? Create one squeeze page with a killer offer on it. Then launch PPC, banner ads, articles, press releases, forum posts, blog comments, SEO, videos, guest blog posts, interviews, etc. Get creative and work hard.
If you’re looking for a quick fix silver bullet way to generate leads, you’re in danger of having your lunch eaten by the entrepreneur who is just willing to do the real work.
If you’re going to win, you need to give yourself enough chances to win. Think bigger.
8. Think 4 Steps Ahead
When opportunities show up at your door, always be asking yourself, “How can I leverage or repurpose this to create 10 times the impact with the same amount of work?” Did you get asked to be interviewed on a blog? Can you secure the non-exclusive rights to turn that interview into a product or a lead generating “freemium” for you? To fully develop the potential of all the opportunities you have, you have to be thinking ahead. It’s a habit that takes time to develop.
Start today.
9. Link Bait Works
I’m not talking about the link bait that comes from attacking someone or publishing something sensational. Link bait doesn’t need to be controversial, it just needs to be valuable.
Could you publish something TRULY valuable and timeless for your prospects? Ask yourself this: “What could I create that my prospects would consider in their best interest to spread around to OTHER prospects?”
Again, think four steps ahead… you don’t just want leads, you want sales. So work backwards from the sale: select your offer, consider any things that have to happen to remove obstacles to the sale, create your linkbait, etc.
I recommend this advice because I’ve ignored it more times than my ego would ever allow me to admit.
10. Don’t Let Shiny Objects Take You Away from the Basics
Success comes from executing the basics WELL. Generating leads is not complicated, it takes 3 things:
a great offer
putting that offer in front of the right people
a reason to act now
Rinse and repeat.
About the Author: Jason Leister is the Editor of "The Client Letter," the daily e-letter from , where he helps independent professionals create success.
If you enjoyed this article, enter your email below to get free updates!
Interested in Thesis?
Thesis is more than just a WordPress theme—it’s a community of wonderful people who are dedicated to building better websites. Buy Thesis today to put your website on the cutting edge, and you’ll also be able to learn from the Thesis pros in our members-only forums!

我要回帖

更多关于 if you can see 的文章

 

随机推荐