Sign in to follow this  

Enum's

This topic is 4817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, My first post here, I've recently started learning C++ in order to start developing games. I'm fed up of writing commercial financial/stock/database packages and want to move to a different area like gaming (and also moving from Delphi to C++ at the same time). I've been gonig through a book "Learn C++ in 21 days" which is a good introduction to C++ I think. I've been trying to find a good community of C++ developers but haven't found that many comprehensive online resources yet. I'll post what I've got, from one of the example projects:
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
     enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

     Days DayOff;
     int x;

     cout << "What day would you like off (0-6)? ";
     cin  >> x;
     DayOff = Days(x);

     if (DayOff == Sunday || DayOff == Saturday)
           cout << "\nYou're already off on weekends!\n";
     else
           cout << "\nOkay, I'll put in the vacation day that " << Days(x) << ".\n"; // Days(x) will not
                                                                                     // convert to Day Names
      return 0;
}



As you can see, I can't get
Quote:
Days(x)
to print out the name of the week. It just prints the number. Am I looking in the wrong place for this? Also the book doesn't say what
Quote:
Days DayOff;
actually does.

Share this post


Link to post
Share on other sites
So how would I do the reverse, give the number and get the text back. In other languages I'd have made an array and done it that way but I figured this might be more efficient.

And in the code I put what does the "Days DayOff;" line do. I appreciate your help.

Share this post


Link to post
Share on other sites
you can think (kinda ) of enum like aliases for a number it's not suposed to be text but simply a substiture for that value as an example with your enum when you get a value from 0 to 6 for the day it makes more readable to have pseudocode that looks like this

if today is saturday or sunday then cout<<"weekend";
else cout<<"work days"

then it does to write code that looks like

if today is 5 or 6 then cout<<"weekend"
else cout<<"work days"

so enum are just a way to make numbers correspond to a word (once again kinda) in a way to make your code cleared but in no way does it give make those things text so basicly when you do cout<<saturday it is the same thing as doing cout<<5; wich is why you end up with a number on screen

enum are just here to make your code more readable but the execution of your program will be the same as if you used the corresponding

as for writing days as text i suggest not trying to see how to do it just keep reading your book untill you get to strings if you want to do text it's kinda boring to learn c++ in the order makes book but it makes more sence in the end so even if something sound more trivial to do then what you're doing now don't rush to it it might actually be harder & get your confused

Share this post


Link to post
Share on other sites
Thanks guys, and thanks ranakor for that great explanation. The book isn't very comprehensive and didn't explain that fully.

Can you recommend any new C++ books that are good and possible containing a syntax listing/reference with it for what comes with the standard libraries. <iostream.h> is reference alot in the books I've seen but that's deprecated and it's now <iostream> (atleast according to the compilers I'm using)

Share this post


Link to post
Share on other sites
personally i bought C++ primer plus by stephen prata sams publishing & i like it (bought it because a buddy wich i'm trying to teach c++ over the net used it ) if you have questions i'm on icq atm as ranakor (194-662-921) i'll gladly help you with my limited knownledge as some stuff tend to be easier to explain with fast ask / answer then with long text (& well i'm awfully bored anyway lol)

Share this post


Link to post
Share on other sites
try this :

enum DayIndex {
Monday = 0,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};
std::string DayNames[7] = {
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
};

DayIndex day = Tuesday; // for example
cout << DayNames[day];




Share this post


Link to post
Share on other sites
Most people use a map or something like it to solve this problem.

This is not the best code but it gets the point across:

#include <iostream>
#include <string>
#include <map>

enum eDay { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
std::map<enum eDay, char*> sDay;

int main()
{
sDay[Sunday] = "Sunday";
sDay[Monday] = "Monday";
sDay[Tuesday] = "Tuesday";
sDay[Wednesday] = "Wednesday";
sDay[Thursday] = "Thursday";
sDay[Friday] = "Friday";
sDay[Saturday] = "Saturday";

eDay day = Wednesday;

std::cout << "day is " << sDay[day] << "\n";

return 0;
}



Often it's wrapped in a class to tighten the association of the two types and hide the initialization.

Share this post


Link to post
Share on other sites

This topic is 4817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this