Simple Chatbot Application in C++
Chatbot is a chatting application which reads the user input, find the appropriate response from the response database. It retrieves it from the database and give it as response to the user.
Major steps are given below.
- Read the input from the user.
- Search it with the database.
- Finds the proper answer.
- Reply it back to the user.
Let us create a Simple Chatbot application in C++.
There are two functions in this program. One is main() and
next one is toLowerCase(String s1).
‘toLowerCase(String s1)’ makes the string to lowercase
letters.
‘main()’ function is used to read the input from the user.
‘getLine()’ is used to read the input.
Based on the inputs, the request is searched according to
the options. Finally,it displays the output.
Here, we named the ChatBot name as Chaty.
Code:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
//This function makes the input to lowercase
string toLowerCase(string s1) {
transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
return s1;
}
int main() {
cout <<
"Hi I'm your Chatbot Chaty: Welcome to the world of Chaty. Type 'bye' to
exit.\n";
string usrip;
while (true) {
cout <<
"You: ";
getline(cin,
usrip);
string ip =
toLowerCase(usrip);
if (ip ==
"hi" || ip == "hello") {
cout
<< "Chaty: Hi there! How are you Feeling Today?\n";
} else if (ip
== "Ya.i am fine" || ip == "i am good") {
cout
<< "Chaty:Nice to hear from you \n";
} else if (ip
== "who was the father of computer?") {
cout
<< "Chaty:Charles Babbage \n";
} else if (ip
== "bye") {
cout
<< "Chaty: Thank you choosing me! Bye..Have a nice day.\n";
break;
} else {
cout
<< "Chaty: Sorry, I dont know about this.\n";
}
}
return 0;
}
Output:
Here is the output.
Hi I'm your Chatbot Chaty: Welcome to the world of Chaty.
Type 'bye' to exit.
You: hi
Chaty: Hi there! How are you Feeling Today?
You: i am good
Chaty:Nice to hear from you
You: who was the father of computer?
Chaty:Charles Babbage
You: bye
Chaty: Thank you choosing me! Bye..Have a nice day.
Hope, this code is useful to you. Keep coding!!!!!
Comments
Post a Comment