[英]In C++, how should I go about parsing an integer nested within a string, where the integer is of variable length?
[英]C++ How do I go about setting a point in a string array to an enum variable
所以我試圖做的是用一副紙牌隨機填充一個字符串數組。 我試圖通過“隨機”創建一個介於 0-51 之間的數字來做到這一點,這樣我在該位置的數組將等於枚舉套裝后跟枚舉值。 因此,查看我的代碼,您可以看到我在數組中設置值的方式會導致錯誤。 我如何在保持枚舉的同時完成此任務?
class DeckOfCards{
private:
string deck[52];
int sCount = 0;
int vCount = 0;
enum Suits{hearts, diamonds, clubs, spades};
enum Value{two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace};
public:
DeckOfCards(){
deck[52];
}
void shuffle(DeckOfCards noShuff){
srand(time(nullptr));
for(int i = 0; i < 52; i++){
deck[rand() % 52] = Suits[sCount] + Value[vCount];
sCount++;
vCount++;
}
}
};
您的第一個問題是您試圖錯誤地引用Suites
和Value
的枚舉值。 Value[0]
不會編譯,但Value::two
會。 由於這不是枚舉 class,因此0
也等同於Value::two
(正如1
等同於Value::three
)。
如果不解決代碼中的其他問題(例如sCount
和vCount
不受Suits
和Value
的大小限制),您可以嘗試deck[rand() % 52] = static_cast<char>(sCount + vCount)
。
但是,我強烈建議您將“卡片”抽象為 class 而不是使用std::string
(見下文)。
class Card {
...
};
class DeckOfCards{
private:
Card deck[52]; <-- Deck of "Cards"
...
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.