arrays - C++ Enum values from string input -
been @ 1 while now, appreciate help.
i'm create , object card
enum values rank
, suit
based on string input. string in format of '2c, ad' etc. 2 clubs , ace diamonds respectively.
i tried using linear search function found elsewhere on stackoverflow, it's in there returns -1 because doesn't seem getting right type of input.
i've been able convert other way using char * array, can't life of me opposite way around. see below implementation:
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 convert enums 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 , destructors card(); ~card(); ///accessors rank getrank(); suit getsuit(); string getsuitstring(); string tostring(); ///no longer used, left 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
#include "card.h" #include <string> #include <stdlib.h> #include <cstring> #include <iostream> #include <sstream> using namespace std; /**constructors*/ ///noarg constructor 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, used during testing has been reimplemented * overloaded operator<< below */ string card::tostring(){ string cardvalues; cardvalues += rankstrings[rank]; cardvalues += suitstrings[suit]; return cardvalues; } /**mutators*/ ///create new card given rank , suit card::card(rank cardrank, suit cardsuit){ rank = cardrank; suit = cardsuit; } int card::linearsearch (const char **array, const char *searchkey, int arraysize) { (int = 0; < arraysize; ++i) { if (strcmp(array[i], searchkey) == 0) return i; } // didn't find searchkey in array return -1; } ///sets card rank , 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 doesnt work in method below int index = linearsearch(rankstrings, c, 13); cout << index << endl; } /**operators*/ ///functor compare 2 cards value bool card::operator()(card* card1, card* card2){ return card1->getrank() > card2->getrank(); } ///puts string representation of input card on output stream ostream& operator<<(ostream& out, card& card){ out << rankstrings[card.rank] << suitstrings[card.suit]; return out; }
the problem taking address of '2' in "2c" , using if complete c string. when use strcmp in linearsearch trying match whole string ("2c") , fails.
one way around change suitstrings
or rankstrings
char
instead of char *
, involve changing other methods well.
another way copy char working seperate 1 character string strcmp work. should it:
char rankstr[2]; rankstr[0] = cardstr[0]; rankstr[1] = '\0'; // null terminated. int index = linearsearch(rankstrings, rankstr, 13);
yet way change linearsearch @ first character of both strings.
Comments
Post a Comment