Artificial Intelligence TechC++Introduction to C++Learn C++

How To Make A Simple But Powerful Chat Bot In C++

How can I make a simple but powerful chat bot in a C++ app? Where should I start to build a chat bot? Can I develop applications with chat functionality like Siri, Google Assistant and other AI based chat bots with C++ Builder? How can I start to develop a natual language processing NLP based application? What is the basic concept to make a simple chat bot?

Technology moves fast and technologies like artificial intelligence and other support systems for humanity are rapidly evolving into being useful. On the other hand, I see that many chat bots of web pages are really bad and don’t fulfil the simple needs of the webpage’s users.

This example below is good for beginners and kids. It is also good for professionals who wants to develop an advanced chat bot in C++ Builder.

How to develop a chat bot in C++?

In this article we show how C++ Builder makes it easy to develop a simple chat bot in C++. It is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

How to create a chat bot in C++?

Here are the full instructions showing how to create a chat bot in C++ that works cross-platform on Windows, Android, iOS, Linux and macOS.

In our example we will use UnicodeStrings as input and as an output which means our example chat bot supports all worldwide languages. Note that C++ Builder also supports both Left To Right and Right To Left languages. C++ Builder makes it easy to get important things like the Regional system settings, Locale information, keyboard settings and other details like IME Mode and keyboard type.

Out chatbot will be designed to understand English but you can modify in your language with the few changes. Now Let’s start!

  1. In RAD Studio, C++ Builder, use the File ->New menu to create a new Multi-Device Application C++ Builder. This means we will create FireMonkey FMX Application that can be compiled for the Windows, iOS, Mac-OS and Android.
  2. Save all Units and Project files to a folder (i.e. MyChatBot)
  3. Add a Label (TLabel) and Align it to the top. When it is selected, set its text to your chatbot name (i.e. My Chat Bot) and use larger size font from the Object Inspector panel. This will display our chatbot title.
  4. Add a Layout (TLayout) and add Edit (TEdit) inside. Align Layout1 to the bottom and align Edit1 to the Client. This Edit1 will be used as an input UI element.
  5. Finally add a Memo (TMemo), this will be used as an output window.
  6. So we have two main components, the Edit1 to use as a input element and the Memo1 as an output element.

7. First let’s create our simple chat bot with a analyze_string() function, To help beginners, let’s make a very simple analyzer. We will create a parrot chat bot, every as string inputs will be replied as an output text in Memo1. If you are in form design, press F12 to switch to coding mode, and write this to then end of the cpp lines.

8. Select Edit1 in Form Design and go to Events tab in Object Inspector and find OnKeyDown event. Doouble click to it to create OnKeyDown event in header and in cpp file. Be sure that it is the event of Edit1.

9. Method should be TForm1::Edit1KeyDown(). In this step, we will detect key presses inside the editor, and if it is a return /enter key we will analyze the text written. Todo this first we will use switch() function for Key press and we will check if the case is VK_RETURN which means enter key is pressed. If it is we will printout the Edit1->Text into the Memo1 as a user input and we will start to analyze this Edit1->Text by using our analyze_string() function. Finally we don’t need input we can clear the Edit1->Text for the new inputs. Otherwise old input will remain.

Modify it as below,

How To Make A Simple But Powerful Chat Bot In C++ The chatbot in action

How can we check for words in a string in C++?

There is a Pos Method of a UnicodeString, returns character index at which specified substring begins. Pos returns the character index in the UnicodeString instance at which the substring subStr begins, where 1 is the index of the first character, 2 is the index of the second character, and so on. If the substring is not contained in the UnicodeStringPos returns 0.

Here is a example how to use it,

UnicodeStrings has a lot of useful properties and methods to operate in modern strings. Pos() method, is one of this useful method to search UnicodeString in a UnicodeString.

Pos Method of a UnicodeString, returns character index at which specified substring begins. Pos returns the character index in the UnicodeString instance at which the substring subStr begins, where 1 is the index of the first character, 2 is the index of the second character, and so on. If the substring is not contained in the UnicodeStringPos returns 0.

Simply we can check words and we can design answers as in example below,

We go into more detail on how to detect and manipulate words in string in C++ in this blog article: How To Operate On The Words In A String In C++

How to enhance your chat bot with Artificial Intelligence technologies?

This C++ chat bot can be enhanced by adding the ability to analyze the words typed using techniques such as NLP (Natural Language Processing), DL (Deep Learning) other Artificial Intelligence related methods and analysis. Using things like NLP your application may answer questions better since it will have a greater understanding of what is being entered by the user. The most used NLP methods are Named entity recognition (NER), Sentiment Analysis, Text summarization, Aspect Mining using Part-of-Speech Tagging, and Topic Modelling Methods like,

How to enhance your C++ chat bot with databases ?

You can hold your chatbot data in a database. C++ Builder has a great official database component, the FireDAC component pack. FireDAC is one of the great components for database connections that comes with RAD Studio, C++ Builder and Delphi. FireDAC is a Universal Data Access library for developing applications for multiple devices, connected to enterprise databases. With its powerful universal architecture, FireDAC enables native high-speed direct access from Delphi and C++Builder to InterBase, SQLite, MySQL, SQL Server, Oracle, PostgreSQL, DB2, SQL Anywhere, Advantage DB, Firebird, Access, Informix, DataSnap and more, including the NoSQL Database MongoDB.

FireDAC is a Universal Enterprise Data Connectivity

How To Make A Simple But Powerful Chat Bot In C++ FreDAC schematic

To use FireDAC with C++ Builder, be sure that your RAD Studio, C++ Builder version has support for this component. We highly recommend here C++ Builder 10.x or above because of improvements on database connections. If you don’t have this component in your version there is a Trial version of FireDAC that you can test and then you can purchase if it meets your needs. In a new C++ Builder Project, VCL or FMX) you can drag and use its components on your forms. Most experienced programmers prefer to add a new DataModule to their project.

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?

C++C++17C++20Introduction to C++Language FeatureLearn C++Syntax

Learn How To Use Clamp (std::clamp) In Modern C++ 17 and Beyond

C++C++11C++14C++17C++20Learn C++

What Is The Priority Queue (std::priority_queue) In Modern C++?