Thread: Converting to values in an Enum Type

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    7

    Converting to values in an Enum Type

    Hi,


    I am trying to define a card class for a card game and am having trouble connecting the values provided to the constructor to some predefined enum types.


    If I defined two enum types to be:


    Code:
    enum Rank {Two, Three, [..other cards..] Jack, Queen, King, Ace};
    enum Suit {Spades, Diamonds, Clubs, Hearts};
    And a constructor which took in two chars representing the rank and the suit (because the user would enter it this way via standard input):


    Code:
    Card (Rank r, Suit s);

    e.g. Card(J,D) -> Is internally equivalent to Card(Jack, Diamonds)


    How do I make that connection?


    I am also given:

    Code:
    string Card::RankName = "23456789TJQKA";
    string Card::SuitName = "SDCH";
    Any help would be much appreciated!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    All sorts of ways. Probably the simplest is to create a second constructor that accepts two char's and does the mapping. With C++-11 you can use a delegating constructor. If your compiler does not support delegating constructors, simply use a (private) helper function that can implement the common details from both constructors.

    If you're happy to expose the user of your class to the need for mapping (which is rarely a good idea, and more indicative of a lazy programmer than anything else) create a helper function so they can create a Card using Card(MapRank('J'), MapSuit('D'));

    Actually doing the mapping (eg switch statements, or by using std::map's) is implementation detail.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    One easy way to do the conversion from char to enum value is
    Code:
    Rank rankFromChar(char r) {
        return static_cast<Rank>(RankName.find(r));
    }
    Since RankName is a string, you can find the position of r in the string, and cast that index to an enum value.

    This is without error checking. You may also want to make sure the position isn't std::string::npos, which would mean the character wasn't found....
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why do the enum values this way??
    By dayalsoap in forum C Programming
    Replies: 1
    Last Post: 06-10-2011, 11:43 AM
  2. Printing values from Enum
    By Raskalnikov in forum C Programming
    Replies: 8
    Last Post: 02-16-2009, 10:28 PM
  3. How to pass enum values
    By Bargi in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2007, 02:55 AM
  4. different values in while using enum?
    By blackgold>> in forum C++ Programming
    Replies: 1
    Last Post: 04-26-2004, 03:35 PM
  5. Converting type string to type const char*
    By rusty0412 in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2003, 05:59 PM

Tags for this Thread