Hello everyone,

Currently, I am trying to make a smaller version of the famous Eliza bot. I have written a program that can understand and put out replies, but if I want to make it bigger then the code will be thousands and thousands of lines long. And all on one file. That would not only be very confusing, but it would also be difficult to do. So if you guys understand what my problem, can you give me some advice on making my program more efficient and more understandable. Here is my code.

#include <cstdlib>
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <string.h>
#include <string>
#include <windows.h>
#include <fstream>
#include <tchar.h>
#include <dos.h>
#include <conio.h>
#include <unistd.h>

// a few global variables 

     char str[50]; // this one is for the responses, the 50 is a random number,I needed something big
     int redo;     // for restarting the speech thing
     int fan, pan, tan, lan; // all of these are necessary for the bot to choose a proper output. I couldn't think of anything so I just wrote wierd names.
     int somethang; // this is to make sure that the first_response is not outputted except only the first time
     
using namespace std;

void open_Something()
{  
     cout << "Enter something to open: ";
     
     char way[50];
     cin.getline (way,50);
     char *path = way;
        
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line)
        path,        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
    
}

    

int main()
    {
       
    somethang = 1;
    if (somethang == 1)
    {
        
    string first_resp[5] = {
            "Hello.\n",
            "Hi!\n",
            "Whazz up\n",
            "So, how's it going?\n",
            "Talk to me\n"
            };
     
     // number generator
     
     int which_Num2;
     
     unsigned seed = time(0);
     srand(seed);
     
     which_Num2 = rand() % 5;
     
     cout << first_resp[which_Num2];
     cin.getline (str,50);  
}
    
    do
     {
                        
     char *pch = NULL;
     char *pch1 = NULL;
     char *pch2 = NULL;
     char *pch3 = NULL;
     char *pch4 = NULL;
                        
     redo = 0;
     if (somethang == 0)
     {
     string second_resp[5] = {
            "Got anything else to say?\n",
            "That is it, nothing else?\n",
            "What else do you want to know about me?\n",
            "So, what's up?\n",
            "Keep talking, I am not finished.\n"
            };
     
     // number generator
     
     int which_Num;
     
     unsigned seed = time(0);
     srand(seed);
     
     which_Num = rand() % 5;
     
     cout << second_resp[which_Num];
     cin.getline (str,50); 
     }
     
   //  
     
     pch = strstr (str,"open");
   
     if (pch != NULL)
     {
        open_Something();
        pch == NULL;
        somethang = 0;
        redo = 1;
     }
     
   //  char *pch1 = NULL;
     
     
     pch1 = strstr (str,"what");
     if (pch1 != NULL)
        fan = 1;
     pch1 = strstr (str,"is");
     if (pch1 != NULL)
        fan = 1;
     pch1 = strstr (str,"your");
     if (pch1 != NULL)
        fan = 1;
     pch1 = strstr (str,"name");
     if (pch1 != NULL)
        fan = 1;
     
     if (fan == 1)
     {
        cout << "My name, you say\n"
             << "It is Jubajee\n"
             << "Cool eh?\n";
        somethang = 0;
             redo = 1;
             fan = 0;
     }
     
     
     pch2 = strstr (str,"smart");
     if (pch2 != NULL)
        pan = 1;
     pch2 = strstr (str,"are you ");
     if (pch2 != NULL)
        pan = 1;
     pch2 = strstr (str,"intelligent");
     if (pch2 != NULL)
        pan = 1;
     pch2 = strstr (str,"dumb");
     if (pch2 != NULL)
        pan = 1;
     
     if  (pan == 1)
     {
         cout << "What kind of a question is that\n"
              << "I am talking to you aren't I?\n"
              << "btw I am 255 lines of pure geniusness.\n";
        somethang = 0;
              redo = 1;
              pan = 0;
     }
     
     
     pch3 = strstr (str,"bye");
     if (pch3 != NULL)
        tan = 1;
     pch3 = strstr (str,"goodbye");
     if (pch3 != NULL)
        tan = 1;
     pch3 = strstr (str,"cya");
     if (pch3 != NULL)
        tan = 1;
     pch3 = strstr (str,"bye");
     if (pch3 != NULL)
        tan = 1;
     pch3 = strstr (str,"see you later");
     if (pch3 != NULL)
        tan = 1;
        
     if  (tan == 1)
     {
         cout << "Goodbye\n"
              << "It was nice talking to you \n";
        somethang = 0;
              Sleep(1500);   
              return 0;
     }
     
     
     
     pch4 = strstr (str,"how old ");
     if (pch4 != NULL)
        lan = 1;
     pch4 = strstr (str,"age");
     if (pch4 != NULL)
        lan = 1;
     pch4 = strstr (str,"are you young");
     if (pch4 != NULL)
        lan = 1;
     pch4 = strstr (str,"when were you born");
     if (pch4 != NULL)
        lan = 1;
     pch4 = strstr (str,"born");
     if (pch4 != NULL)
        lan = 1;
        
     if  (lan == 1)
     {
         cout << "Me, I am not that old\n"
              << "About 1 or 2 weeks old. \n";
              somethang = 0;
              Sleep(1500);
              redo = 1;   
     }
     
     // if the input is not part of the database this happens
     if (tan == 0 && pan == 0 && fan ==0 && lan == 0)
     {
             cout <<"I don't quite understand what you are saying\n"
                     <<"My database is not fully completed.";
             redo = 1;
             }
     
     //the bottom curly brace is for the do-while loop
     }while (redo == 1);
     
    system("PAUSE");
    return 0;
}

Thanks in advance for the help.

Edited 6 Years Ago by some: n/a

You can not hardcode every string.

You need a data file you can put strings into, with each string you should put data defining its fan, pan, tan, lan membership (a 1 or 0 for each one or a single field of bits defining all of them for instance.

Then alter your program to work off the file. Initially you could read the file and store it in memory, later when the file gets big you may need to work directly off the file.

That way you should be able to build a program that has exactly the same functionality as your current program but reads a data file for all strings. Once you have a program like that making it do more is a simple matter of adding data to the file, no more coding required.

Maybe you could even expand the program to ask questions when it get unrecognised words to enable it to categorise them and add then to the data file so it becomes a learning program.

You can not hardcode every string.

You need a data file you can put strings into, with each string you should put data defining its fan, pan, tan, lan membership (a 1 or 0 for each one or a single field of bits defining all of them for instance.

Then alter your program to work off the file. Initially you could read the file and store it in memory, later when the file gets big you may need to work directly off the file.

That way you should be able to build a program that has exactly the same functionality as your current program but reads a data file for all strings. Once you have a program like that making it do more is a simple matter of adding data to the file, no more coding required.

Maybe you could even expand the program to ask questions when it get unrecognised words to enable it to categorise them and add then to the data file so it becomes a learning program.

I think I understand what you are trying to say, but what I don't get it is how I would separate each set of responses and I don't understand how I would get my program to open a file with a lot of responses and choose the appropriate one. Any help would be appreciated.

Well you could simple create a line in the file for each string consisting of

StringInQuotes,fan,pan,tan,lan

When you want to test fan responses you read and parse each line of the file. If the line has fan set to 1 then you process the string using the strstr call as you have been doing. If the result is 1 output the appropriate response. If the result is 0 you continue to the next line of the file. If you get to the end of the file without getting a positive hit then output your "don't understand" response.

Alternitively, test the string on each line, if you get a positive match check the fan,pan,tan,lan values to see which response to give otherwise move onto the next line. You could condense fan,pan,tan,lan into a single response value that gives the response to give if the string tests true.

Just open the file and read it a line at a time. Process the line, if you get a response close the file again otherwise move onto the next line. If you get to the end of the file with no response output "don't understand"

This article has been dead for over six months. Start a new discussion instead.