DisplayCards Func:出错

时间:2014-12-10 02:21:33

标签: c++

所以我有这个程序,要求用户输入5张猜测,一张18张牌。在猜测它们之后,我必须显示用户猜测和种子生成的计算机卡。这是我的代码,我收到此错误

309 34  [Error] conversion from 'std::string [19] {aka std::basic_string<char> [19]}' to non-scalar type 'std::string {aka std::basic_string<char>}' requested
void DisplayCards(int* user, int* generatedCards)
{
    cout << "Users Guess" << "\t\t\t" << "Generated Cards" << endl;
    for (int i = 0; i < 5; ++i)
    {
        // Get the names of the choices from the deck
        std::string UserChoice = CARDS;
        std::string GeneratedCard = CARDS;
        // print the names side by side
        cout << UserChoice << "\t\t\t" << GeneratedCard << endl;
    }

我的CARDS是全球性的:

std::string CARDS[19] = {"nothing","red circle","red square","red triangle","blue circle","blue square",
 "blue triangle","yellow circle","yellow square","yellow triangle","orange circle",
 "orange square" ,"orange triangle","purple circle","purple square",
 "purple triangle","green circle","green square","green triangle"};

std::string  CARDS[19]={"nothing","red circle","red square","red triangle","blue circle","blue square",
 "blue triangle","yellow circle","yellow square","yellow triangle","orange circle",
 "orange square" ,"orange triangle","purple circle","purple square",
 "purple triangle","green circle","green square","green triangle"};

1 个答案:

答案 0 :(得分:0)

您的错误将通过解决这些陈述来解决:

std::string UserChoice = CARDS;
std::string GeneratedCard = CARDS;

您需要索引数组以获取这些变量的正确值。

您的函数参数是包含五个猜测和生成的卡的数组,因此您的函数将变为:

void DisplayCards(int* user, int* generatedCards)
{
    cout << "Users Guess" << "\t\t\t" << "Generated Cards" << endl;
    for (int i = 0; i < 5; ++i)
    {
        // Get the names of the choices from the deck
        std::string UserChoice = CARDS[user[i]];
        std::string GeneratedCard = CARDS[generatedCards[i]];
        // print the names side by side
        cout << UserChoice << "\t\t\t" << GeneratedCard << endl;
    }
}

函数参数是指向数组的第一个元素的指针,可能是通过调用我无法看到的函数传入的。您可能希望包含更多错误检查,但它的要点是您可以使用i计数器作为索引号,以与普通数组相同的方式索引输入参数。如果在运行代码时阵列的大小不合适,您将获得虚假数据并可能出现分段错误,但这取决于您自己学习。

希望这有帮助。