尝试编译此代码时遇到问题

时间:2018-09-20 03:09:27

标签: c++

尝试编译此解决方案时遇到问题。
我对C ++相当陌生,但是我一直在通过练习来教自己。请批评!

我收到了大量错误,但是我花了数小时在互联网上查找我所缺少的内容,但是我似乎无法破解错误消息。

最大的错误似乎是它无法识别我的cin>>命令

我想知道是否可以在每个案例中放置一个if语句,并且可能更易于阅读,但是我一直认为那是不合法的。

/////////////////////////////////////////////////

#include <iostream>
#include <string>
using namespace std;

int main()
{
   int gameNumber;
   string response;

    cout << "\tPlease choose the number for the game you would like to play: \n" << endl;
    cout << "\t\t1. Shenmue 1 and 2.\n" << endl;
    cout << "\t\t2. Witcher 3.\n" << endl;
    cout << "\t\t3. Dragonball Xenoverse 2.\n" << endl;
    cout << "\t\t4. God of War.\n" << endl;
    cout << "\t\t5. Splinter Cell.\n" << endl;
    cout << "\t\t6. The Sims 3.\n" << endl;
    cout << "\t\t7. Life is Strange 3.\n" << endl;
    cout << "\t\t8. God of War 7.\n" << endl;
    cin >> gameNumber;

    switch (gameNumber)
    {
        case 1:   
                cout << "Good choice!" << endl;
                cout << "Which console did you play it one?" << endl;
        cin >> response;
            break;
        case 2:
                cout << "This one is really long!" << endl;
                cout << "Who was your favorite character?" << endl;
        cin >> response;
            break;
        case 3:
                cout << "I've beat this one a bunch!" << endl;
                cout << "Who do you battle with the most?" << endl;
        cin >> response;
            break;
        case 4:
                cout << "Best game ever!" << endl;
                cout << "Which God of War is your favorite?" << endl;
        cin >> response;
            break;
        case 5:
                cout << "When's the last time you heard this name?" << endl;
                cout << "Have you actually heard of this game?" << endl;
        cin >> response;
            break;
        case 6:
                cout << "Why can't you kill anyone anymore?" << endl;
                cout << "What was your favorite death?" << endl;
        cin >> response;
            break;
        case 7:
                cout << "You wish this was out." << endl;
                cout << "Do you wish this game was out?" << endl;
        cin >> response;
            break;
        case 8:
                cout << "You really wish this was out!" << endl;
                cout << "What could they possibly make this game with?" << endl;
        cin >> response;
            break;
        default:
                cout << "You're dumb, click the list!" << endl;
            break;
    }

    return 0;
}

此编辑后的代码可以正确编译,并且可以在此阶段执行我想要的操作。感谢您的所有反馈。我接受了你们的建议并指出并修复了它!再次感谢你们。

2 个答案:

答案 0 :(得分:3)

您的大多数程序都很好。主要问题是您实际上似乎只想要一个字符串,而不是50个字符串的数组,因此您想将此string response[50];更改为:string response;

另一个问题是,在一个地方您无意中使用了<<,而您无疑打算使用>>,因此:cin << response;必须是cin >> response;

通过这些更改,它可以编译并似乎可以完成我希望您浏览一下代码后可能想要的工作。我想我应该补充一点,但这并不意味着代码是按照我希望看到的方式真正编写的。就我个人而言,我可能会写得更像这样:

#include <iostream>
#include <string>
#include <vector>

struct Game {
    std::string name;
    std::string resp;
    std::string question;

    std::string ask() {
        std::cout << resp << "\n";
        std::cout << question;
        std::string ret;
        std::cin >> ret;
        return ret;
    }
};

int main()
{
    std::vector<Game> games
    {
        { "Shenmue 1 and 2.", "Good choice!", "Which console did you play it on?"},
        { "Witcher 3.", "This one is really long!", "Who was your favorite character?"},
        { "Dragonball Xenoverse 2.", "I've beat this one a bunch!",  "Who do you battle with the most?" },
        { "God of War.", "Best game ever!", "Which God of War is your favorite?" },
        { "Splinter Cell.", "When's the last time you heard this name?", "Have you actually heard of this game?"},
        { "The Sims 3.", "Why can't you kill anyone anymore?", "What was your favorite death?"},
        { "Life is Strange 3.", "You wish this was out.", "Do you wish this game was out?"},
        { "God of War 7.","You really wish this was out!", "What could they possibly make this game with?" }
    };


    std::cout << "\tPlease choose the number for the game you would like to play: \n";
    for (int i = 1; i < games.size(); i++) {
        std::cout << i << ") " << games[i].name << "\n";
    }

    int gameNumber;
    std::cin >> gameNumber;
    games[gameNumber].ask();
}

我对此并不完全满意,但这至少是一种改进。

答案 1 :(得分:2)

这里发生了很多事情,但是我将从阻止它编译的问题开始。如您所见,cin调用无法正常工作-更确切地说,operator>>无法解决。

我猜您正在从仍然有很多旧C知识的源中学习C ++,因为您对response的定义是两者的混合体,但最终都处于中间地位那不会编译。在C语言中,您会将此字符串声明为字符数组,可能像您一样将其大小设置为50,但是其语法实际上是char response[50](尽管这对于出于多种原因,它们与您当前所做的事情并不相关)。您的string response[50]没有声明长度为50的字符串,而是声明了50个字符串的数组!希望这可以清楚说明为什么operator>>不起作用:它知道如何将某些内容放入字符串中,但是不知道如何将其放入字符串数组中。

这里的简单解决方案是只声明一个string,如其他答案所示。由于C ++字符串是自动调整大小的,因此它将自动处理长度,您不必担心会随意选择最大值。

您似乎还怀疑,switch和if语句本质上是在做相同的事情,因此您可以将它们组合在一起(并且应该这样做以提高可读性)。在这种情况下,您甚至根本不需要if:只需在break;之前将cout / cin调用移至switch例中即可,它应该可以完成您的期望。

最后,通常不建议编写using namespace std;,在您使用所有标准库内容之前,只需编写std ::会更清晰,更不易出错。这使读者和编译器的意图都更加清楚,我保证您会很快习惯它。

祝你好运!