纸牌游戏中的结构向量在此之后不显示纸牌,不确定是否已创建牌组

时间:2019-05-02 17:40:10

标签: c++ c++11

我正在用C ++做一个基本的纸牌游戏,我知道我的纸牌已经被创造出来了,我只是不知道它们是否被放入了向量中。

我尝试使用函数来传回信息,我对其进行了测试,没有经过改组,以查看它是否至少检测出正确的猜测。

struct card
{
   string cardFace, cardSuit;
};


int main()
{

    vector<card> deck;
    card currentCard;
    string input;

    for (int i = 0; i < 4; i++)
    {
        string suits[4] = { "Hearts", "Diamonds", "Spades", "Clubs" };

        for (int j = 1; j < 13; j++)//13 cards per suit
        {
            card a;

            if (j == 1)
                a.cardFace = "Ace";

            if (j == 11)
                a.cardFace = "Jack";

            if (j == 12)
                a.cardFace = "Queen";

            if (j == 13)
                a.cardFace = "King";

            else
                a.cardFace = to_string(j);

            a.cardSuit = suits[i];
            deck.push_back(a);
        }
    }

    random_shuffle(deck.begin(), deck.end());

    currentCard.cardFace = deck[0].cardFace;
    currentCard.cardSuit = deck[0].cardSuit;

    if(input == currentCard.cardFace)
    cout << currentCard.cardFace << " is the right answer!" << endl;

    return 0;
}

我希望输出为:Jack是正确的答案! 但我知道:是正确的答案!

2 个答案:

答案 0 :(得分:2)

我在IdeOne上尝试过您的程序版本:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct card
{
   string cardFace, cardSuit;
};


int main()
{
    vector<card> deck;
    card currentCard;

    for (int i = 0; i < 4; i++)
    {
        string suits[4] = { "Hearts", "Diamonds", "Spades", "Clubs" };

        for (int j = 1; j < 13; j++)//13 cards per suit
        {
            card a;

            if (j == 1)
                a.cardFace = "Ace";

            if (j == 11)
                a.cardFace = "Jack";

            if (j == 12)
                a.cardFace = "Queen";

            if (j == 13)
                a.cardFace = "King";

            else
                a.cardFace = to_string(j);

            a.cardSuit = suits[i];
            deck.push_back(a);
        }
    }

    random_shuffle(deck.begin(), deck.end());

    currentCard.cardFace = deck[0].cardFace;
    currentCard.cardSuit = deck[0].cardSuit;

    cout << currentCard.cardFace << " " << currentCard.cardSuit << " is the right answer!" << endl;

    return 0;
}

它按预期工作。

答案 1 :(得分:0)

我在您的程序中看到的唯一输出是一个if值,它将始终为false,因为您从未设置input。您似乎有兴趣验证甲板是否已成功构建。为此,请将其添加到main live example here

的底部
for (auto& c : deck) {
    std::cout << c.cardFace << " " << c.cardSuit << '\n';
}

以上显示了deck中的所有卡。虽然我推荐many more changes