首页 > 解决方案 > 将用户输入与数组进行比较适用于一个函数,但不适用于另一个函数

问题描述

我又来了。我正在开发一个基于 cli 的聊天机器人,我试图让它回答诸如“谁让你?”之类的问题。我已经解决了“你好吗?”这个问题,而制作它的功能只是你好吗?ofc 具有不同的数组、不同的数组名和不同的函数名。它编译没有问题,我在 main() 中调用该函数并引用用户输入字符串。询问时间是有效的,也询问它是如何工作的。那是我的代码:

    #include <iostream>
#include <ctime>

using namespace std;

void howareyouq(string const& user_input);
void whattimeisit(string const& user_input);
void whomadeu(string const& user_input);

int main()
{
    string user_input; //creating userinput variable
    cin >> user_input; //writing user input to user_input 

   howareyouq(user_input);
   whattimeisit(user_input);
   whomadeu(user_input);
}          

void howareyouq(string const& user_input)
{
    string howruq[] = {"how are you?", "hru?", "how r u?", "hru"}; //array for the valid thingies
    for (int i=0; i!=4; i++)
    { 
        if (user_input == howruq[i]) {  //if the array contains something equal to user_input, this gets executed
            cout << "Thanks, Im fine!" << endl; //the answer
        }
    }
}

void whattimeisit(string const& user_input)
{
    string thetimepls[] = {"timepls", "what time is it?", "how late is it?", "whats the time?", "time?"};
    for (int i=0; i!=5; i++) { 
        if (user_input == thetimepls[i]) {
            time_t now = time(0);
            char *date = ctime(& now);
            cout << "The local date and time : " << date << endl;
        }
    }
}

void whomadeu(string const& user_input)
{
    string whomadeyoua[] = {"who made you", "who made you?", "who programmed u?", "who programmed you", "which person made u?", "which person made u"}; //array for the valid thingies
    for (int i=0; i!=6; i++)
    { 
        if (user_input == whomadeyoua[i]){  //if the array contains something equal to user_input, this gets executed
            string whomadeuq[4] = {"who made u?", "Who made you?", "who made you", "Who made you"}; //array for the valid thingies
            if (user_input == whomadeuq[4]) {  //if the array contains something equal to user_input, this gets executed
                cout << "Big Smarty."; //the answer
            }
        }               
    }
}

标签: c++linuxchatbot

解决方案


推荐阅读