Converting to values in an Enum Type

This is a discussion on Converting to values in an Enum Type within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to define a card class for a card game and am having trouble connecting the values ...

  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,206
    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%.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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, 09: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21