Thread: Chat bot brain ideas

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Code:
    // Function for opening/writing to a file.
    void WriteToFile ( char *file_name, char *text )
    {
    
    	// Open file with ios::app to append
            ofstream the_file ( file_name, ios::app );
    
    	// Check for file
    	if ( !the_file.is_open () ) {
    
    		cout << "Failed to open " << file_name;
    		exit ( 1 );
    
    	}
    
    	// Output the text and a newline
            the_file << text << endl;
    
    	the_file.close ();
    
    }
    The ios::app goes to the end of the file to write so it doesnt overwrite whats already in the file.

    Also the only thing my code example will do is write "Hello" to the file if you type "hello" and quite if you type "quit".

    Just add more conditions to it and you will get more stuff.

    Code:
    do {
    
    	getline ( cin, the_string );
    
    	if ( the_string == "hello" ) WriteToFile ( "C:/CPU.txt", "Hello" );
            else if ( the_string == "something" ) WriteToFile ( "C:/CPU.txt",  "Why something?" );
    
    } while ( the_string != "quit" );
    Now using this method it is case sensitive. So "Hello" is different than "hello".

    I would suggest you just play around with it until you understand what is going on with it. When you go "Ah ha!" then you will be ready for more

    EDIT:

    This code will be more efficent because you wont be opening and closing the file everytime you write to it.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main ()
    {
    
        string the_string;
    
        ofstream the_file ( "c:/CPU.txt", ios::app );
    	
        cout << "Talk to me." << endl;
    
        do {
    
    	getline ( cin, the_string );
    
    	if ( the_string == "hello" ) the_file << "hi" << endl;
    	else if ( the_string == "quit" ) the_file << "bye" << endl;
    	else the_file << "I dont understand" << endl;
    
        } while ( the_string != "quit" );
    
        the_file.close ();
        
        return 0;
    
    }
    Run that code and type

    hello
    llamma
    quit

    Then go to C:\ and you will see CPU.txt, open it and look.

    OMG
    I just realised what you want to do. I am so sorry. doh im so slow.

    You want the information to be in a text file!!!!

    So you want it to search the text file for certain keywords found in the user input string and write the apropriate reponses.

    Jeez im so sorry, I am on some cold medication and the room is spinning so forgive my stupidity.

    Ok well my code still aplies in theory but Im not familiar with searching files for text. Let me go look up some stuff.
    Last edited by Vicious; 10-01-2004 at 02:30 PM.
    What is C++?

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    One way is not to search the file per se. You can read the file one input at a time and compare it to some desired value before going to next item in file, or you can read entire file into a container and search container---tends to be faster than the first method, or you can construct the file so that it is completely predictable with regard structure and use fseek(), etc to appropriately position file pointer to read out just a given piece of data (presumes you know where to look, too!).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Chat Bot Help!!
    By arealfind08 in forum C Programming
    Replies: 1
    Last Post: 03-09-2009, 06:52 AM
  2. Chat BOT Sample
    By RoD in forum C++ Programming
    Replies: 19
    Last Post: 11-04-2002, 04:51 AM
  3. Re: Girlfriend Post
    By PsychoBrat in forum A Brief History of Cprogramming.com
    Replies: 57
    Last Post: 05-13-2002, 06:11 AM
  4. Msvc Aim Chat Bot ???
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 09-19-2001, 08:46 AM
  5. Chat bot for AIM
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 09-08-2001, 02:10 PM