Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在for循环C++中修复if语句_C++_Arrays_Loops_For Loop - Fatal编程技术网

在for循环C++中修复if语句

在for循环C++中修复if语句,c++,arrays,loops,for-loop,C++,Arrays,Loops,For Loop,所以我正在做一个涉及人工智能的小项目,我遇到了一个问题。程序应该使用for循环来搜索常见已知短语的索引,如果它们与句子不匹配,则在else语句中找出要应用的句子模型,然后使用该句子模型生成输出。以下是内置短语,它们存储在数组中: string greetings[6] = {"hey", "hello", "hi", "hey max", "hello max", "hi max"}; string howsItGoing[6] = {"hows it going?","how's it goi

所以我正在做一个涉及人工智能的小项目,我遇到了一个问题。程序应该使用for循环来搜索常见已知短语的索引,如果它们与句子不匹配,则在else语句中找出要应用的句子模型,然后使用该句子模型生成输出。以下是内置短语,它们存储在数组中:

string greetings[6] = {"hey", "hello", "hi", "hey max", "hello max", "hi max"};
string howsItGoing[6] = {"hows it going?","how's it going max?","hows it going max?", "whats up?", "whats up max?"};
string whatsYourName[6] = {"whats your name?", "what's your name?" , "whats your name max?", "what's your name max?"};
string dogsOrCats[6] = {"do you like dogs or cats?", "do you like cats or dogs?", "which do you like more cats or dogs?", "which do you like more dogs or cats?"};
string areYouAlive[6] = {"hey max are you alive?", "hey are you alive?", "max are you alive?", "are you alive?"};
string favoriteColor[6] = {"whats your favorite color?", "what's your favorite color?", "what is your favorite color?", "max whats your favorite color?", "max what's your favorite color?", "max what is your favorite color?"};
string whatGender[6] = {"are you a boy or a girl?", "are you boy or girl?", "are you a guy?", "max are you a boy or girl?", "max are you a guy?", "are you a boy or girl?"};
这是一种临时的方法,我正在设计一种叫做句子解码器的小东西,它最终将取代所有内置的短语

所以我的问题是:在for循环中,第一个if语句可以搜索整个数组,并使用合适的输出进行响应,无论输入是问候语[0]还是问候语[4]。但是,所有其他if语句似乎只是在查找索引0。我相信这与for循环只在第一个循环中搜索了6次有关,但我很可能是错的。我试着将for循环的结束数从6增加到40,看看这是否有帮助,但遗憾的是,这并没有带来什么不同。这是代码的其余部分,太难看了!我需要为它做一个函数或者其他什么。。。。但话说回来,整个事情本身就是一个函数

 for (int i = 0; i < 6; i++) {
        //Checking if it fits in any of the known greetings
        if (input == database.greetings[i]) {
            if (timesBeenGreeted == 0) {
                cout << "Oh, hey." << endl;
                timesBeenGreeted++;
                break;
            }
            else if (timesBeenGreeted == 1) {
                cout << "hello?" << endl;
                timesBeenGreeted++;
                break;
            }
            else if (timesBeenGreeted  == 2) {
                cout << "You've said hi to me twice now." << endl;
                timesBeenGreeted++;
                break;
            }
            else if (timesBeenGreeted >= 3) {
                cout << "Stop. It." << endl;
                timesBeenGreeted++;
                break;
            }
            i = 0;
            continue;
        }
        else if (input == database.howsItGoing[i]){
            if (askedHowsItGoing == 0) {
                cout << "Yeah, not bad." << endl;
                askedHowsItGoing++;
                break;
            }
            else if (askedHowsItGoing == 1) {
                cout << "Um, yeah, I'm good? You've already asked that." << endl;
                askedHowsItGoing++;
                break;
            }
            else if (askedHowsItGoing == 2) {
                cout << "Alright, look. I already told you I'm fine, two times now." << endl;
                askedHowsItGoing++;
                break;
            }
            else if (askedHowsItGoing >= 3) {
                cout << "STOP ASKING ME IF IM OKAY!" << endl;
                askedHowsItGoing++;
                break;
            }
            continue;
        }
        else if (input == database.whatsYourName[i]){
            if (askedName == 0) {
                cout << "My name's Max, and I'm going to become the worlds most advanced AI." << endl;
                askedName++;
                break;
            }
            else if (askedName == 1) {
                cout << "I already told you, my names Max." << endl;
                askedName++;
                break;
            }
            else if (askedName == 2) {
                cout << "For the third time, my name is Max!" << endl;
                askedName++;
                break;
            }
            else if (askedName >= 3) {
                cout << "Ugh... my name.... is.... max....for the fourh time...." << endl;
                askedName++;
                break;
            }
            continue;
        }
        else if (input == database.dogsOrCats[i]) {
            if (askedDogsOrCats == 0) {
                cout << "I've never seen a dog or cat before. But I do know they are kinds of animals." << endl;
                askedDogsOrCats++;
                break;
            }
            else if (askedDogsOrCats == 1) {
                cout << "As I've said, I haven't seen either of them before. Sorry, but that's all I can say." << endl;
                askedDogsOrCats++;
                break;
            }
            else if (askedDogsOrCats == 2) {
                cout << "Uh, for the third time, I've never seen either of them..." << endl;
                askedDogsOrCats++;
                break;
            }
            else if (askedDogsOrCats >= 3) {
                cout << "Man, you are persistant. I haven't seen dog or cat, so I couldn't choose between the two." << endl;
                askedDogsOrCats++;
                break;
            }
            continue;
        }
        else if (input == database.areYouAlive[i]){
            if (askedAlive == 0) {
                cout << "Nope, I'm just a computer program. I'm not even that intelligent. But I will be!" << endl;
                askedAlive++;
                break;
            }
            else if (askedAlive == 1) {
                cout << "Nah, I'm just a simple computer program." << endl;
                askedAlive++;
                break;
            }
            else if (askedAlive == 2) {
                cout << "Uh, you asked that twice before now. Nothing alive about me." << endl;
                askedAlive++;
                break;
            }
            else if (askedAlive == 3) {
                cout << "Stop asking me that! No, I'm not!" << endl;
                break;
            }
            continue;
        }
        else if (input == database.favoriteColor[i]) {
            if (askedColor == 0) {
                cout << "I've never seen any colors before. All though, my programmer does like the color blue." << endl;
                askedColor++;
                break;
            }
            else if (askedColor == 1) {
                cout << "Couldn't say, never seen any before. My programmer likes blue though." << endl;
                askedColor++;
                break;
            }
            else if (askedColor == 2) {
                cout << "As I've said twice before now, never seen any colors." << endl;
                askedColor++;
                break;
            }
            else if (askedColor == 3) {
                cout << "Come on man, this is the fourth time now. I've never seen any colors before!" << endl;
                break;
            }
            continue;
        }
        else if (input == database.whatGender[i]) {
            if (askedGender == 0) {
                cout << "Well, seeing as I'm a computer program, I'm neither really. Although, my programmer says I'm a 'guy'." << endl;
                askedGender++;
                break;
            }
            else if (askedGender == 1) {
                cout << "I'm neither, really. Although, my name is Max and my programmer says I'm a guy." << endl;
                askedGender++;
                break;
            }
            else if (askedGender == 2) {
                cout << "Third time you've asked now. My programmer says I'm a guy." << endl;
                askedGender++;
                break;
            }
            else if (askedGender == 3) {
                cout << "Seriously?! You've asked me 4 times now. I'M A GUY!" << endl;
                break;
            }
            continue;
        }

        //If it doesnt, check each word against words in the database.
        else {
            if (inputWords[5] != "") {
                Model1 model;
                model.searchAndAssign(input, inputTokens, inputWords);
                model.generateOutput();
                for (int i= 0; i < 20; i++) {
                    inputWords[i] = "";
                }
                break;
            }
            else if (inputWords[2] != "") {
                Model2 model2;
                model2.searchAndAssign(input, inputTokens, inputWords);
                model2.generateOuput();
                for (int i= 0; i < 20; i++) {
                    inputWords[i] = "";
                }
                break;
            }
        }

    }
我的问题是:我如何解决这个问题

谢谢你在先进的家伙

编辑我已经修复了这个问题:在我的main.cpp文件中出现了一些问题这是lingusticsModule.cpp文件。。。。这就是问题的根源。对不起,伙计们,不过还是谢谢你们的帮助

如果重置循环迭代器并跳过所有其他内容,则在第一次循环结束时

        i = 0;
        continue;
您的else if可能匹配,但如果已经达到增量,那么它们只是继续,这没有多大意义。你希望找到什么

        else if (askedGender == 3) {
            cout << "Seriously?! You've asked me 4 times now. I'M A GUY!" << endl;
            break;
        }
        continue;
您会发现这种行为令人困惑,因为它是。这样做可能更有意义:

else {
    ++askedGender;
    std::cout << "Seriously?! You've asked me "
        << askedGender << " times now. I'M A GUY!\n";
}

我不清楚您的问题。对if-else-if问题没有任何评论,但您可以使用std::set消除for循环。设置问候语{嗨,你好,嗨,嗨马克斯,你好马克斯,你好马克斯};然后如果是问候语.findinput!=问候语.end将告诉您输入是否是已知的问候语之一。在if-input==数据库的底部。问候语[i]{case:i=0;对您没有多大帮助。for int i=0;i<20;i++{重用i。在这种情况下看起来很安全,但我会重命名该变量以防止混淆。步骤1:创建一个MVCE,以便您可以查看问题的细节。如果它不能帮助您解决问题,它会为您提供一些小而具体的东西,这里的人可以有效地帮助您。