Thread: Enum help

    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    175
    Rep Power
    0

    Question Enum help


    Hey all i am trying to figure out why my code below is not working.
    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <cctype>
    #include <algorithm>
    
    using namespace std;
    
    enum thePlanets {mercury, venus, earth, moon, mars, jupiter, saturn, uranus, neptune, pluto};
    
    int main()
    {
    	thePlanets planetNameMode;
    	string planetName;
    	double planetNum;
    
    	cout << "Enter a name of a planet (Mercury, Venus, Earth," << endl;
    	cout << "Moon, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto)" << endl;
    	cin >> planetName;
    
    	transform(planetName.begin(), planetName.end(), planetName.begin(), (int(*)(int)) tolower);
    	planetNameMode = planetName;
    
    	switch (planetNameMode)
    	{
    	case mercury:
    		planetNum = 0.4155;
    		break;
    	case venus:
    		planetNum = 0.8975;
    		break;
    	case earth:
    		planetNum = 1.0;
    		break;
    	case moon:
    		planetNum = 0.166;
    		break;
    	case mars:
    		planetNum = 0.3507;
    		break;
    	case jupiter:
    		planetNum = 2.5374;
    		break;
    	case saturn:
    		planetNum = 1.0677;
    		break;
    	case uranus:
    		planetNum = 0.8947;
    		break;
    	case neptune:
    		planetNum = 1.1794;
    		break;
    	case pluto:
    		planetNum = 0.0899;
    		break;
    	}
    The error i get is:
    weightAndPlanet.cpp(28): error C2440: '=' : cannot convert from 'std::string' to 'thePlanets'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

    error on line : planetNameMode = planetName;
    How can i solve this problem of mine?

    Thanks!
    David
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,782
    Rep Power
    4302
    enum actually creates you a bunch of variables called mercury, venus, earth, moon etc. and assigns them values: mercury = 0, venus = 1, earth = 2...

    So when you do something like this:
    Code:
    planetNameMode = mercury;
    you're essentially assigning it to 0, since the value of mercury is 0

    Now planetName is a string type, so you can't assign it to an enumerated variable. That's what the compiler is complaining about.

    You could perhaps declare an array of planet names:
    Code:
    string thePlanetNames[] = {"mercury", "venus", "earth", "moon", "mars", "jupiter", "saturn", "uranus", "neptune", "pluto"}; /* moon isn't a planet! */
    
    /* write some code here that looks up the planet names in the above array and
        returns the appropriate thePlanets value */

    Comments on this post

    • sizablegrin agrees : Reality intrudes ;)
    • StealthRT agrees : Thanks for the help there Scorpions4ever!
    Last edited by Scorpions4ever; September 17th, 2008 at 08:24 PM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    175
    Rep Power
    0
    Ah thanks for that info Scorpions4ever. I understand now. A enum is pretty much an array. I still don't see how useful an enum can be to a program... But to each his own i guess!

    But anyways, thanks! :)

    David

    Comments on this post

    • sizablegrin disagrees
  6. #4
  7. Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jun 2005
    Posts
    5,929
    Rep Power
    4856
    Have you actually investigated what "enum" means?? I'm voting "no". Have you further questions??
    Write no code whose complexity leaves you wondering what the hell you did.
    Politically Incorrect DaWei on Pointers Grumpy on Exceptions
  8. #5
  9. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,809
    Rep Power
    1574
    Originally Posted by StealthRT
    Ah thanks for that info Scorpions4ever. I understand now. A enum is pretty much an array.
    Well... no. That's exactly what an enum is not.

    In C/C++, an enum is a way of declaring a series of related constants, and/or a variable type which can only hold those values. The constants have associated integer values, but are not integers per se. For example, the declaration
    Code:
    enum stoplight {GREEN, YELLOW, RED};
    ... creates a constant GREEN equal to 0, a constant YELLOW equal to 1, and a constant RED equal to 2. You can then declare
    (in C) an enum variable
    Code:
    enum stoplight fourth_and_main;
    Which can only be assigned one of those values. In C++, you can drop the enum part. You can also declare just the constants alone, and even set
    Code:
    enum {CLUBS, DIAMONDS, HEARTS, SPADES };
    which is (almost) the same as
    Code:
    const int CLUBS = 0, 
              DIAMONDS = 1, 
              HEARTS = 2, 
              SPADES = 3;
    You can also set specific values to them, or set a non-zero starting point for a series of values:
    Code:
    enum  {FOO = 5, BAR, BAZ = 23, QUUX, FRINK};
    Which is roughly equivalent to
    Code:
    const int FOO = 5, 
              BAR = 6, 
              BAZ = 23, 
              QUUX = 24, 
              FRINK = 25;
    OK, you may be wondering why you would bother with this if that's all it does. Well, it has some advantages over declaring individual constants, the first being that you can declare a set of sequential named constants without having to know or care about the specific values of the constants themselves. For the stoplight, chances are it doesn't matter of GREEN == 42, YELLOW == 69 and RED == 666; all that is important to you is that GREEN comes before YELLOW, and YELLOW comes before RED. If you have a sequence like this, and somewhere along the line a new values in interdigitated between two of the values, you don't need to re-number the whole thing - the compiler handles it for you.

    It also means that you can have a variable which is limited in the values it can take:
    Code:
    fourth_and_main = 105; // compile-time error
    You can use an enumerated value as an int, but (in C++ at least) you can't use an int to set the value of an enum variable. Even if the value was 1 (== YELLOW), it would be an error, since while the value of YELLOW is 1, 1 is not the constant YELLOW.

    It can also be useful when dealing with an indexed array representing a fixed, named value. To go back to your original example, if you have
    Code:
    enum thePlanets {MERCURY, VENUS, EARTH, MARS, CERES, 
                     JUPITER, SATURN, URANUS, NEPTUNE, 
                     PLUTO, HAUMEA, MAKEMAKE, ERIS}; 
    // if you're including Pluto, you might as well 
    // include the other dwarf planets as well
    
    string thePlanetNames[] = {"Mercury", "Venus", "Earth",  "Mars", "Ceres", 
                               "Jupiter", "Saturn", "Uranus", "Neptune", 
                               "Pluto", "Haumea", "Makemake", "Eris"};
    you can then get the string representation of the planet (i.e., it's name) by using an enum value or variable as the index:
    Code:
    thePlanets where_am_I = EARTH; // you're starting off right here
    
    // ... do something ...
    
    cout << "I am currently on " << thePlanets[where_am_I];
    This would work even if where_am_I was changed to, say, ERIS fnord or something. Note that this is still a bit fragile - you have to make sure that the order of enumerated constants matches the order of the indexed array - but it can save you a good deal of hassle. This is just one example of how you could use them; there are others you could probably think of yourself. HTH.
    Last edited by Schol-R-LEA; September 25th, 2008 at 11:08 PM.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2007
    Posts
    19
    Rep Power
    0
    Originally Posted by StealthRT
    Ah thanks for that info Scorpions4ever. I understand now. A enum is pretty much an array. I still don't see how useful an enum can be to a program... But to each his own i guess!

    But anyways, thanks! :)

    David
    You can use enum in switch statements. Better readability I suppose.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2008
    Location
    Procyon
    Posts
    10
    Rep Power
    0
    It's also usefull in this situation; instead of using this:
    Code:
    #define A 0
    #define B 1
    #define C 2
    #define D 3
    You can do:
    Code:
    enum {
       A = 0,
       B,
       C,
       D
    };
    :)

IMN logo majestic logo threadwatch logo seochat tools logo