c++ - 将用户输入与数组进行比较适用于一个函数,但不适用于另一个函数
问题描述
我又来了。我正在开发一个基于 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
}
}
}
}
解决方案
推荐阅读
- php - 在访问器中覆盖 Laravel Eloquent 模型在使用属性时返回 0
- ruby-on-rails - 在 Rails 中为更简洁的 URL 自定义嵌套路由?
- c++ - 获取函数返回的一些垃圾
- segmentation-fault - 在 runc 容器中执行二进制文件时出现分段错误
- javascript - 如果 Promise 中有一个空对象,则停止执行 Await 的方法,但不应返回错误并执行其他任务
- ios - OpenGL 在 IOS 12 中已弃用,如何在 ios 12+ 设备上安全地继续使用 openGL?
- python - Pandas:Groupby 并在组内使用条件进行迭代?
- powershell - 使用 Powershell 在虚拟机上远程运行可执行文件
- android - 为什么 Android Studio 3.1.4 出现渲染问题?
- drupal-8 - Drupal 8 - 如何以编程方式在管理表单显示下启用自定义字段?