Я хочу написать базовый сценарий, который позволяет осуществлять обмен данными между пользователем и компьютером. Например
USER: what's your name?
BOT: my name is John
USER: what's today's weather?
BOT: the weather is sunny
Код, который у меня пока есть …
#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace std;
string respond(map<string, vector<string> > responses, string message)
{
if(responses.find(message) != responses.end()){
return responses[message][0];
} else {
return responses["default"][0];
}
}
int main(){
map<string, vector<string> > responses;
vector<string> temp;
temp.push_back("my name is John");
temp.push_back("they call me John");
temp.push_back("I go by John");
responses["what's your name?"] = temp;
vector<string> temp1;
temp1.push_back("the weather is sunny");
temp1.push_back("it's cloudy today");
responses["what's today's weather?"] = temp1;
vector<string> temp2;
temp2.push_back("default message");
responses["default"] = temp2;
while(1){
cout << "Write your message to the bot and press ENTER" <<
endl;
string user_msg;
cout << "USER: ";
cin >> user_msg;
if(user_msg == "quit"){
break;
}
else{
string temp = respond(responses, user_msg);
cout << temp << endl;
}
}return 0;
}
Прямо сейчас, когда я набираю один из responses[]
(То есть. what's your name?
/what's today's weather?
), Я вернусь…
Write your message to the bot and press ENER
USER: what's your name?
default message
Write your message to the bot and press ENER
USER: default message
Write your message to the bot and press ENER
USER: default message
Write your message to the bot and press ENER
USER:
Любая помощь, чтобы исправить это будет высоко ценится. Спасибо
Вы неправильно получаете строку в своем вводе. В методе respond
, значение message
просто what's
, Это происходит потому, что вы используете cin
который не читает ввод после того, как он встречает пробел. Вы можете использовать что-то вроде getline
вместо.
cout << "USER: ";
// Do this
std::getline (std::cin, user_msg);
Вот рабочий пример для вашего кода: http://cpp.sh/5dnrh
Других решений пока нет …