Devdutt Shenoi 28 October 2016

How to build a Chatbot in C++

Program file

Header files

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fstream>// for saving and accessing data file.
#include <windows.h>// for system('cls');
#include <stdio.h>
	#include <time.h>// for accessing system time.

Don't forget to define the namespace as standard.

using namespace std;

Class defenitions

entry class

Define a class 'entry' which is used as a framework for the entries into the binary file. Create an object of this class 'no1' also. This is for later use.

class entry {
	char ans[20], user[20];
	char que[30];
	void re(){
	void ideal(){
	void getUser(char u[20]){
} no1;
bot class

Define a class 'bot' which holds details pertaining to the Artificial Intelligence program.

class bot {
	char iq[30], re[20], ideal[20], userName[10];
	//Constuctor to make object of bot.
	bot(char n[20]){
		cout<"\n\n\t\tHey there, this is Devdutt\n\n\nBefore you start to chat with my bot, can I get your name?\n" << endl;
	void reply(char input[30]);
	void rename(char n[]){

Class method defenitions

Define the bot's 'reply' method to answer queries based on data stored in a binary file or otherwise ask the user to give a fitting reply to the question, so that the bot can learn.

void bot::reply(char input[30]){
	fstream M;
	// Open the file for use."memory.bin", ios::in | ios::binary);
	// Search through the file until end is reached.
	while( M ) {*)&no1,sizeof(no1));
		strcpy(iq,no1.que); // Get next question for comparison.
		// Check whether the question from memory is what we are looking for.
		if(iq == input) {; // If so, get related reply.
	// If reply couldn't find input in textfile close it!
	// Open memory.dat for output, and use append at end of file."memory.bin", ios::out | ios::app| ios::binary);
	strcpy(no1.que,input);   // Record initial input in memory.
	cout << "I don't know what you mean when you say \'" << input << "\', what should I say?\n\tIDEAL REPLY (PLEASE INPUT) : ";
	no1.ideal();    // Get the ideal output.
	no1.getUser(userName); // Save name of person who modified database.
	M.write((char*)&no1,sizeof(no1));    // Write the ideal output to memory.
	M.close();    // Close the file!

Main Function

Given inside the main function is the actual program. Access functions defined in the global scope to run the utility programs.

// These are activated when the User wants.
void quiz();
void joke();
void showTime();
void dataRe();
int ask(char in[]);
int i,n;
char name[20];
bot AI(name);

Generate AI object of bot class. This will be accessed from hereforth.

Main Function

Define the main() function. It is the portion of the program which does the work of deciding the execution sequence of other functions.

int main() {
	char input[30];
	//Char variable to store answer for Exit.
	for(;; i++) {
		// Prompt User for input.
		cout<<name<<": ";
		cout<<"AI: ";
		// Check whether the user wanted to exit i.e. n==-1.
		if(n==-1) return 0;
	return 0;

Function Defenitons


Define a function that will handle questions that can be used to trigger an applet program such as a game of quiz.

int ask(char in[30]){
        char y;
        //Give maximum number of fallbacks to greetings, to not miss.
        if(strcmp(in,"Hello")==0||strcmp(in,"hello")==0||strcmp(in,"Hi")==0||strcmp(in,"hi")==0) cout << "Hello there ";
        // Let the User decide whether to exit the program.
        else if(strcmp(in,"exit")==0||strcmp(in,"Exit")==0||strcmp(in,"X")==0) {
                cout << "\n\t\t\tAre you sure?(Y/N) \n\t\t\tYou have asked "<<i<<" questions.\n";
                if(y=='y'||y=='Y') return -1;
        else if(strcmp(in,"name")==0||strcmp(in,"Name")==0) cout <<"\n\t\tMy name is V!\n\t\tcreated by Devdutt Shenoi ";
        else if(strcmp(in,"data")==0) dataRe();
        else if (strcmp(in,"Game")==0||strcmp(in,"game")==0) quiz();
        else if (strcmp(in,"joke")==0||strcmp(in,"Joke")==0) joke();
        else if(strcmp(in,"What is the time?")==0||strcmp(in,"time")==0||strcmp(in,"Time")==0) showTime();
        /*Only if the User doen't want to exit should his question go to the AI.
           Pass User's input to the AI and let it respond.*/
        else AI.reply(in);
        return 0;
Definitions pertaining to random functions.

Define the functions whose prototypes were given before. These will do certain jobs that the user wants.

1 .quiz(): A game which asks questions
void quiz() {
        int r = rand() % 4;
        char in[20];
        switch(r) {
        case 0: cout<<"\nWho is the PM of India?\n";
                if(strcmp(in,"Narendra Modi")==0||strcmp(in,"Modi")==0) {
        case 1: {cout<<"\nWho is the President of India?\n";
                 if(strcmp(in,"Pranab Mukherjee")==0)
        case 2: cout<<"\nWho am I?\n";
                if(strcmp(in,"Artificial Intelligence")==0||strcmp(in,"AI")==0||strcmp(in,"V!")==0) {
        case 3: cout<<"\nWho is Steve Jobs?\n";
                if(strcmp(in,"Artificial Intelligence")==0||strcmp(in,"AI")==0||strcmp(in,"V!")==0) {
        default: exit(0);//Add more questions
        cout<<"\nWrong answer";
2. joke(): A function to say jokes as per a random number algorithm
void joke() {
	int r = rand() % 4;
	switch(r) {
		case 0: cout<<"\nAsk SiRi 'What is 10^200?' only if you are a musician."; break; case 1: cout<<"\nYou:'Who is Indian?'\nSiRi:'You must be Trump'";
		case 2: cout<<"\nOnce a friend said 'I have 8GB RAM, 2TB HDD, 8core CPU, what do you have?'\nI answered I have a 'mac'";
		case 3: cout<<"\nPhysics students want Newton to have been a Keralite, why?\n Becuase coconut has more 'm', giving a larger 'F'.";
		default: exit(0); //Add more jokes
3. showTime(): A function to say the time by printing it into the console
void showTime(){
	time_t rawtime;
	struct tm * timeinfo;
	time ( &rawtime );
	cout<<"The current date/time is: "<< ctime(&rawtime);
4. dataRe(): Displays previous questions and their respective answers, saved in memory file
void dataRe(){
	ifstream in;"memory.bin",ios::binary);
	while( in ) {*)&no1, sizeof(no1));
		cout<<"Q: ";
		cout<<"\nA: ";;


The above program is a very good representation of the abilities of C++ in Artificial intelligence. Even though the program was very simple, the ability to learn from past experiences is a very basic trait of programs built for machine learning.

Download source code from:

Download actual project report from: OneDrive