C++实现聊天机器人

​#include <iostream>
#include <string>
using namespace std;

int main() {
    string question;
    string answer;

    // Define question and answer list
    string questions[] = {"Hi", "How are you?", "What's your name?"};
    string answers[] = {"Hello", "I'm fine, thanks.", "My name is Chatbot."};

    // Get user input
    while (true) {
        cout << "Ask me a question: ";
        getline(cin, question);

        // Compare user input with questions
        if (question == questions[0]) {
            answer = answers[0];
        } else if (question == questions[1]) {
            answer = answers[1];
        } else if (question == questions[2]) {
            answer = answers[2];
        } else {
            answer = "I don't understand.";
        }

        // Give answer
        cout << answer << endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/SYC20110120/article/details/133431874