我正在製作一個非常簡單的程序,只是一個小聊天機器人AI類的事情,當然,我還有一些代碼,當然,程序的C++。我沒有得到任何錯誤,但是當我運行它時出現一個窗口,說program.exe已停止工作,就像它停止響應。我的代碼是:C++程序已停止工作
#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()方法,因此字典永遠不會被初始化。 – ryanbwork
這聽起來像你沒有運行調試器。你應該運行調試器,因爲它會破壞異常,你可以確切地知道它爲什麼會崩潰。 – evanmcdonnal
什麼是先進的STL和完全無知的STL迷人的組合。我很無禮。看看如何使用'std :: map'和'std :: vector',這會使這段代碼變得容易很多,而且容易出錯。 –