简体   繁体   English

来自字符串输入的C ++枚举值

[英]C++ Enum values from string input

Been at this one for a while now, would appreciate some help. 到现在已经有一段时间了,不胜感激。

I'm to create and object Card with enum values Rank and Suit based on a string input. 我要基于字符串输入来创建和反对具有枚举值RankSuitCard The string will be in the format of '2C, AD' etc. for TWO CLUBS and ACE DIAMONDS respectively. 该字符串将分别为两个CLUBS和ACE DIAMONDS的格式为“ 2C,AD”等。

I tried using a linear search function I found elsewhere on StackOverflow, it's in there but it just returns -1 because it doesn't seem to be getting the right type of input. 我尝试使用在StackOverflow上其他地方找到的线性搜索功能,该功能在其中,但它只是返回-1,因为它似乎无法获得正确的输入类型。

I've been able to convert the other way using a char * array, but can't for the life of me do the opposite way around. 我已经可以使用char *数组进行另一种转换,但是我一生无法做相反的事情。 See below for implementation: 参见以下实现:

Card.h Card.h

#ifndef _card_h
#define _card_h

#include <string>
#include <iostream>

using namespace std;


enum Rank{  TWO, THREE, FOUR, FIVE, SIX, SEVEN,
            EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE};


enum Suit{  CLUBS, DIAMONDS, HEARTS, SPADES};

//Character sets used to convert enums to strings
static const char * RankStrings[] = { "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A",};
static const char * SuitStrings[] = { "C", "D", "H", "S" };

class Card {

public:

    ///Constructors and destructors
    Card();
    ~Card();

    ///Accessors
    Rank getRank();
    Suit getSuit();
    string getSuitString();
    string toString(); ///No longer used, but left for testing
    int linearSearch(const char**, const char*, int);

    ///Mutators
    Card(Rank rank, Suit suit);
    Card(string cardStr);


    ///Operators
    bool operator()(Card*, Card*);
    friend ostream& operator<<(ostream&, Card&);

private:
    Suit suit;
    Rank rank;
};

#endif // _random_h

Card.cpp Card.cpp

#include "card.h"
#include <string>
#include <stdlib.h>
#include <cstring>
#include <iostream>
#include <sstream>
using namespace std;

/**CONSTRUCTORS*/
///Noarg constructor that sets default card values
Card::Card(){
    suit = CLUBS;
    rank = TWO;
}

Card::~Card(){} //Destructor

/**ACCESSORS*/
Rank Card::getRank(void){
    return rank;
}

Suit Card::getSuit(void){
    return suit;
}

string Card::getSuitString(){
    return SuitStrings[suit];
}

/**From tutorial, was used during testing but has been reimplemented
 * as an overloaded operator<< below
 */
string Card::toString(){

    string cardValues;

    cardValues += RankStrings[rank];
    cardValues += SuitStrings[suit];

    return cardValues;
}

/**MUTATORS*/
///Create a new card with given Rank and Suit
Card::Card(Rank cardRank, Suit cardSuit){
    rank = cardRank;
    suit = cardSuit;
}

int Card::linearSearch (const char **Array, const char *searchKey, int arraySize) {
    for (int i = 0; i < arraySize; ++i) {
        if (strcmp(Array[i], searchKey) == 0)
            return i;
    }

    // We didn't find the searchKey in the Array
    return -1;
}

///Sets card rank and suit based on string input
Card::Card(string cardStr){
    stringstream ss;
    string rankStr;

    char *c = &cardStr.at(0);
    cout << *c << endl; //This prints correct value but doesnt work in method below

    int index = linearSearch(RankStrings, c, 13);
    cout << index << endl;


}



/**OPERATORS*/
///Functor to compare two cards for their value
bool Card::operator()(Card* card1, Card* card2){
    return card1->getRank() > card2->getRank();
}

///Puts a string representation of input card on the output stream
ostream& operator<<(ostream& out, Card& card){
    out << RankStrings[card.rank] << SuitStrings[card.suit];
    return out;
}

The problem is you are taking the address of the '2' in "2C" and then using it as if it is a complete C string. 问题是您要在“ 2C”中获取“ 2”的地址,然后像使用完整的C字符串一样使用它。 Then when you use strcmp in linearSearch it is trying to match the whole string ("2C") and fails. 然后,当您在linearSearch中使用strcmp时,它会尝试匹配整个字符串(“ 2C”)并失败。

One way to get around this is to change your SuitStrings or RankStrings to char instead of char * , but that would involve changing your other methods as well. 解决此问题的一种方法是将SuitStringsRankStrings更改为char而不是char * ,但这也将涉及更改其他方法。

Another way is to copy the char you are working with into a seperate one character string so that strcmp will work. 另一种方法是将要使用的字符复制到一个单独的字符串中,以便strcmp可以工作。 something like this should do it: 这样的事情应该做到这一点:

char rankStr[2];
rankStr[0] = cardStr[0];
rankStr[1] = '\0'; // NULL terminated.

int index = linearSearch(RankStrings, rankStr, 13);

Yet another way is to change linearSearch to just look at the first character of both strings. 另一种方法是将linearSearch更改为仅查看两个字符串的第一个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM