【问题标题】:C++ program has stopped workingC++ 程序已停止工作
【发布时间】:2012-12-13 22:23:03
【问题描述】:

我正在制作一个非常简单的程序,只是一个小聊天机器人 AI 之类的东西,而且我有一些代码,当然是 c++ 用于该程序。我没有收到任何错误,但是当我运行它时会出现一个窗口,说 program.exe 已停止工作,就像它停止响应一样。我的代码是:

#include<iostream>
#include<string.h>
#include<cmath>
#include<vector>
#include<ctime>
#include<conio.h>
#include<algorithm>
#include<cstdlib>
using namespace std;

struct strarray{
   char* array[];
};

struct keyword{
   string keywords;
   string responses[];       
};


keyword * dictionary = new keyword[2];
keyword defaultr;

keyword getMatch(string key);
string sconvert(string con);
void init();
string getResp(keyword key);

bool cont=true;

int main(int argc, char* argv[]){
   string input;
   while(cont){
            getline(cin,input);
            cout << getResp(getMatch(input));
            getch();
            getch();
   }
}

string sconvert(string con){
   con.erase(remove_if(con.begin(), con.end(), ::isspace), con.end());
   con.erase(remove_if(con.begin(), con.end(), ::ispunct), con.end());
   return con;
}

void init(){
   srand(time(NULL));
   dictionary[0].keywords="hello";
   dictionary[0].responses[0]="Hello, how have you been?";
   dictionary[0].responses[1]="Hello, have you missed me?";
   dictionary[0].responses[2]="Hey, how's it going?";
   defaultr.responses[0]="That's interesting, tell me more.";
   defaultr.responses[1]="Please, tell me more.";
}

keyword getMatch(string key){
    for(int i=0; i<sizeof(dictionary); i++){
            if(key==dictionary[i].keywords){return dictionary[i];}
    }
    return defaultr;
}

string getResp(keyword key){
   return key.responses[rand() % sizeof(key)];
}

当我运行它时,它会正常打开,但是当我输入一些内容后它会“停止工作”。有人可以告诉我我需要改变什么,为什么会被感激。

有指针问题吗?或者rand 的东西?我真的很困惑,希望能得到一些关于如何改进这个程序以使其真正发挥作用的建议。

【问题讨论】:

  • 一方面,您永远不会调用您的 init() 方法,因此字典永远不会被初始化。
  • 听起来你没有通过调试器运行。您应该运行调试器,因为它会因异常而中断,您可以找出它崩溃的确切原因。
  • 高级 STL 和对 STL 完全无知的完美结合。我很感兴趣。查看如何使用std::mapstd::vector,这将使这段代码更容易,更不容易出错。
  • 我建议你下载Visual C++,它的调试器要好得多
  • 在关键字结构的定义中,您将响应定义为长度为零的字符串数组。这肯定不是你想要的?

标签: c++ pointers artificial-intelligence chatbot


【解决方案1】:

sizeof(dictionary) 将给出sizeof(keyword*),可能是48,因此您将遍历字典数组的末尾并终止。

最简单的解决方法:定义一个常量来存储数组长度。

const dictionarySize = 2;

并始终使用它。

您还需要将struct keyword 更改为:

struct keyword{
   string keywords;
   string responses[3];       
};

【讨论】:

  • 谢谢你,你确实提供了一些帮助,不像其他人只是批评我而没有给我任何有用的东西。
  • 更好的是,使用 std::vector 或其他知道其大小的容器,而不是数组。
【解决方案2】:

首先你有一个无限循环,所以程序应该永远工作..我看了一眼代码,使用 rand() % sizeof(key) 是错误的,响应不是预先确定的,所以你设置它到一个特定的值,例如

struct keyword {
    string keywords;
    string responses[2];       
};
rand() % sizeof(key.responses)

或者你让你的结构像这样

struct keyword {
    string keywords;
    vector<string> responses;      
};
rand() % key.responses.size()
//After setting the responses by push_back for example

还有其他方法,但这样更安全,不需要内存管理......

【讨论】:

  • 我把它改成了sizeof(key.responses),实现了无限循环,我是故意这么做的,以后再改。我添加了响应数组大小的定义,但仍然出现分段错误。顺便说一句,我编辑了一些东西,肯定是在getmatch函数期间,这里wtf错了,有人告诉我。
  • 写你的新代码..还有一件事最好初始化所有的关键字
猜你喜欢
  • 1970-01-01
  • 2013-11-11
  • 1970-01-01
  • 1970-01-01
  • 2016-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多