I am writing a chatbot and it's database is going to be contained in a .txt file.
Now i have this code:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;




int main()
{
    string line;
    string uinput, resp;
    char key = line[0];

    ifstream file("t.txt");

    cout<<": ";
    getline(cin, line);

    while(getline(file, line))
    {
        switch(key)
        {
            case 'i':
            {

                if(uinput == line)
                {
                    continue;
                }
            }
            case 'r':
                resp = line;
            break;
            default:
                continue;
            break;
        }
        cout<<resp;
    }
}

This code is suppose to read the filem line by line. And if the virst letter = i; It must check to see if the user's input match that line, else if the first letter = r and the previos line matched the user's input it must read that line into another string and then print it.

The problem is I dont really know how to do this...

So how is this going to be done? (Sample code would be appreciate)

Thanks...

The previous line from where? The previous line that matched from the file? Where are you getting the first letter from? Are you getting it from the user input or from the line you got from the file? Please elaborate a little more with what needs to be done.

uinput :is the users input 

the 'key' is the first letter in the database file:

      iHallo Bot
      rHallo User

That is a very simple example: the program will search for a line and if the key (thats the first letter of the line in the database) is 'i' it will read that line and see if it matches the user's input (uinput). Then it will get the next line begining with 'r' and will read that line into a string called resp (response). Then it will display the resp line. Thus giving a small amount of artificial intellegance.

Something like thisd might help you out

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    fstream fin("textfile.txt");
    string userInput, fileLine, response,temp;
    cout << ": ";
    getline(cin, userInput);
    // read lines from file
    while (getline(fin, fileLine))
    {
        //if you find and i
        if(fileLine[0] == 'i')
        {
            // then search for a line begining with r
            while (getline(fin, fileLine))
            {
                if(fileLine[0] == 'r')
                {
                    response = fileLine;
                    break;
                }
            }
            // no guaranty there is a response
            if (!response.empty())
            {
                cout << response;
                break;
            }
            else
            {
                cout << "no match";
                break;
            }
        }
    }
    cin.get();
    return 0;
}

BTW anyone know how to format code in the correctly now? The code button doesnt highlight anything and I havent figured it out yet. The help on formatting the code doesnt help either.

Thanks, now as you might have noticed it only use exact sentence matching... Now how will i make it to rather use keywords?

I leave that part up to you. You can use the code I provided and change it to do what you want and if you still are having problems then post what you have and ill try to help you out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.