Simple Chatbot using C++

Simple Chatbot using C++ (With Source Code)

Simple Chatbot using C++ (With Source Code)

Hello, coders. Welcome to the codewithrandom blog. In this article, we will learn how to create a  Simple Chatbot is a small virtual assistant program which is written in C++ programming language. It employs text-based input and output, as well as speech recognition and synthesis capabilities through the “espeak” command.

Simple Chatbot using C++

When the program is launched, the user is required to provide a password for authentication. If the proper password is supplied, the program will greet the user according to the current time. It then begins a loop in which it takes user commands and responds with appropriate replies.

Cinema Booking System using C++ (With Source Code )

Features of the Chatbot:

  • This program will understand orders and commands for the user such as greetings, questions regarding the virtual assistant, getting the current date and time, and starting program such as Notepad or visiting websites such as Google, YouTube, and Instagram.
  • It will display or return the response messages for each recognized command and convert the response into voice using the “espeak” command.
  • The program will continue to receive and respond to commands until the user gives an exit command or the program is terminated.

Overview of the UI:

<============================= W E L C O M E=============================><============================= I’M VIRTUAL ASSISTANT =============================><============================= MY NAME IS Aditi =============================>
<============================= I’M HERE TO HELP YOU AND MAKE YOUR LIFE EASY =======================>
  • The user will be asked to enter the password
“=======================”
| ENTER YOUR PASSWORD |
“=======================”
enter your password
  • Then 
How can i help you sir….
Your query ===>
  • The user will enter their query and the result will be displayed
Here is the result for your query ===>

 Simple Chatbot using C++ Source code:

You can directly use this code by copying it in your IDE to understand the working and then can create it by understanding the project.

#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <ctime> // must include this header file to use time function

using namespace std;

// function to wish user according to time
void wishme(){
    // current date  and time based on your system timezone
    time_t now = time(0);
    tm *time = localtime(&now);

    if (time-> tm_hour < 12){
        cout<< "Good Morning Sir"<<endl;
        string phrase = "Good Morning Sir";
        string command = "espeak \"" + phrase + "\"";
        const char *charCommand = command.c_str();
        system(charCommand);
    }

    else if (time-> tm_hour >= 12 && time->tm_hour <= 16){
        cout<< "Good Afternoon sir"<<endl;
        string phrase = "Good Afternoon sir";
        string command = "espeak \""+ phrase + "\"";
        const char *charCommand = command.c_str();
        system(charCommand);
    }
    
    else if (time-> tm_hour > 16 && time->tm_hour < 24){
        cout<< "Good Evening sir"<<endl;
        string phrase = "Good Evening sir";
        string command = "espeak \"" + phrase + "\"";
        const char *charCommand = command.c_str();
        system(charCommand);
    }
}

void datetime(){
    time_t now = time(0);
    char *dt = ctime(&now);
    cout<<"The date and time is "<<endl
        << dt <<endl;
}

int main()
{
    system("cls");

    cout<<"\t\t\t<============================================= W E L C O M E ==========================================>"<<endl;
    cout<<"\t\t\t<============================================= I'M A VIRTUAL ASSISTANT ==========================================>"<<endl;
    cout<<"\t\t\t<============================================= MY NAME IS Aditi ==========================================>"<<endl;
    cout<<"\t\t\t<============================================= I'M HERE TO HELP YOU ==========================================>"<<endl<<endl;

    char password[20]; //to take password
    char ch[100]; // to take command from the user

    do
    {
        cout<<"======================="<<endl;
        cout<<"| ENTER YOUR PASSWORD |"<<endl;
        cout<<"======================="<<endl<<endl;
        string phrase = "enter your password";
        string command = "espeak \"" + phrase + "\"";
        const char *charCommand = command.c_str();
        system(charCommand);

        gets(password);

        STARTUPINFO startInfo = {0};
        PROCESS_INFORMATION processInfo = {0};

        if(strcmp(password, "chauhan")==0){
            cout<<"\n<==================================================================================================>\n\n";
            wishme();
            do{
                cout<<"\n<==================================================================================================>\n\n";
                cout<<endl<<"How can i help you sir...."<<endl<<endl;

                string phrase = "How can i help you sir";
                string command = "espeak \"" + phrase + "\"";
                const char *charCommand = command.c_str();
                system(charCommand);

                cout<<"Your query ===> ";
                gets(ch);
                cout<<endl;
                cout<<"Here is the result for your query ===> ";

                if(strcmp(ch, "hi") == 0 || strcmp(ch, "hey") == 0 || strcmp(ch, "hello") == 0 ){
                    cout<<"Hello sir....."<<endl;
                    string phrase = "Hello sir";
                    string command = "espeak \"" + phrase + "\"";
                    const char *charCommand = command.c_str();
                    system(charCommand);
                }
                
                else if(strcmp(ch, "bye") == 0 || strcmp(ch, "stop") == 0 || strcmp(ch, "exit") == 0 ){
                    cout<<"Good Bye sir, have a nice day!!!!"<<endl;
                    string phrase = "Good Bye sir, have a nice day";
                    string command = "espeak \"" + phrase + "\"";
                    const char *charCommand = command.c_str();
                    system(charCommand);
                    exit(0);
                }
                
                else if(strcmp(ch, "who are you") == 0 || strcmp(ch, "tell me about yourself") == 0 || strcmp(ch, "about") == 0 ){
                    cout<<"I'm a virtual assistant created by Aditi !!!"<<endl;
                    string phrase = "I am a virtual assistant created by Aditi";
                    string command = "espeak \"" + phrase + "\"";
                    const char *charCommand = command.c_str();
                    system(charCommand);
                }

                else if(strcmp(ch, "how are you") == 0 || strcmp(ch, "whatsup") == 0 || strcmp(ch, "how is your day") == 0 ){
                    cout<<"I'm good sir, tell me how can i help you.."<<endl;
                    string phrase = "I'm good sir, tell me how can i help you";
                    string command = "espeak \"" + phrase + "\"";
                    const char *charCommand = command.c_str();
                    system(charCommand);
                }

                else if(strcmp(ch, "time") == 0 || strcmp(ch, "date") == 0){
                    // make function to show date and time
                    datetime();
                }
                   else if(strcmp(ch, "open notepad") == 0){
                    cout<<"openining notepad....."<<endl;
                    string phrase = "opening notepad";
                    string command = "espeak \"" + phrase + "\"";
                    const char *charCommand = command.c_str();
                    system(charCommand);
                    CreateProcess(TEXT("C:\\Windows\\notepad.exe"), NULL, NULL, NULL, FALSE, NULL, NULL, NULL, &startInfo, &processInfo);
                }
              
                    
                

                else if(strcmp(ch, "open google") == 0){
                    cout<<"opening google....."<<endl;
                    string phrase = "opening google";
                    string command = "espeak \"" + phrase + "\"";
                    const char *charCommand = command.c_str();
                    system(charCommand);
                    system("start https://www.google.com");
                }

                else if(strcmp(ch, "open youtube") == 0){
                    cout<<"opeining YouTube....."<<endl;
                    string phrase = "opening youtube";
                    string command = "espeak \"" + phrase + "\"";
                    const char *charCommand = command.c_str();
                    system(charCommand);
                    system("start https://www.youtube.com");
                }

                else if(strcmp(ch, "open instagram") == 0){
                    cout<<"openining instagram....."<<endl;
                    string phrase = "opening instagram";
                    string command = "espeak \"" + phrase + "\"";
                    const char *charCommand = command.c_str();
                    system(charCommand);
                    system("start https://www.instagram.com");
                }
                else{
                    cout<<"Sorry could not understand your query please ty again !!!"<<endl;
                    string phrase = "Sorry could not understand your query please ty again";
                    string command = "espeak \"" + phrase + "\"";
                    const char *charCommand = command.c_str();
                    system(charCommand);
                }


            }while(1);
        }
        else
                {
                    system("cls");

                    cout << "\t\t\t<============================= W E L C O M E=============================>" << endl;
                    cout << "\t\t\t<============================= I'M VIRTUAL ASSISTANT =============================>" << endl;
                    cout << "\t\t\t<============================= MY NAME IS Aditi =============================>" << endl;
                    cout << "\t\t\t<============================= I'M HERE TO HELP YOU AND MAKE YOUR LIFE EASY =============================>" << endl
                         << endl;

                    cout << "======================" << endl;
                    cout << "X Incorrect Password X" << endl;
                    cout << "======================" << endl
                         << endl;
                    string phrase = "Incorrect Password, Please enter correct password";
                    string command = "espeak \"" + phrase + "\"";
                    const char *charCommand = command.c_str();
                    system(charCommand);
                }
    } while (1);
    

    return 0;
}

Now let us understand the code:-

  • We will start by writing the header of the code with the required libraries – iostream (input and output stream), windows.h (for windows specific functionality), stdio.h (standard input /output), string.h (string manipulation) and ctime (to use time function).
  • Now we will create a function called ‘wishme()’ to wish or greet the user according to the current time. The current date and time based on your system time zone will be displayed on the console. It will use the time() function from the ctime library to display the current time. A greeting message will be displayed according to the current time as it will get checked. The function will use espeak command (text-to-speech synthesis) to speak the greeting message using the system function. We will use if else if control statement to check for morning, afternoon or evening displayed according to the current time.
  • The program relies on the external program “espeak” for text-to-speech functionality.
  • The datetime() function will display the date and time using ctime function. It will get the current time using time and then convert it to a string format using ctime.
  • The main() function is where the program will begin. It will first clear the screen using system(“cls”). Then we will display the welcome messages. The program will prompt the user to enter password for authentication. The password in this project is ‘chauhan’. If the entered password is correct (compared using strcmp() function) the program will proceed further, otherwise, a message will be displayed stating incorrect password and then the program will restart.
  • We will call the wishme() function to greet the user according to the current time. Then the system will ask – “How can i help you sir….”. The user will enter the query they want to get answer to. The answer to the query then will be displayed.
  • We will use if-else if-else statements to answer the queries asked by the user. There are predefined queries to which the user can get answers to such as – hi, hey, bye, exit, who are you, how are you, time, date, open notepad, open google and etc. If the query entered in not recognized by system then it will display – “Sorry could not understand your query please ty again !!!”.
  • This sums up our project of Creating Chatbot using C++.

Number List and Operations Project in C++ (With Source Code)

Final Output:-

Here is an example to show how this project works.

Simple Chatbot using C++

Simple Chatbot using C++

Simple Chatbot using C++

Simple Chatbot using C++

Simple Chatbot using C++

Simple Chatbot using C++

Conclusion

We have reached the end of this article and have a lot more projects in C++ coming so stay tuned. We have started with awesome and fun projects for you all to understand C++. Learning C++ by creating fun projects makes learning easy and interesting.

Number Guessing Game using C++ (With Source Code)

If you enjoyed the article and learned something new today, let us know in the comments.

Thank you.

Happy Reading! 🙂

Follow: CodeWithRandom



Leave a Reply