Here is my code, I am trying to make a smaller version of a chatterbot like Eliza. Anyways here it is.

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


// a few global variables 

     char str[50]; // this one is for the responses, the 50 is a random number, needed something big
     int redo;     // for restarting the speech thing
     
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()
    {
    
    do
     {
    
     string resp[5] = {
            "Got anything else to say?",
            "That is it, nothing else?",
            "...Anything else",
            "So, what's up?",
            "Talk to me",
            };
     
     // number generator
     
     int which_Num;
     
     unsigned seed = time(0);
     srand(seed);
     
     which_Num = rand() % 5;
     
     cout << resp[which_Num];
     cin.getline (str,50); 
     
     
     char * pch;
     pch = strstr (str,"open");
   
     if (pch != NULL)
     {
        open_Something();
     }
     
     char * pch1;
     pch = strstr(str,"what, is, your, name,"); 
     if (pch1 != NULL)
     {
        cout << "My name, you say\n"
             << "It is Jubajee\n"
             << "Cool eh?\n";
             redo = 1;
     }
     
     char * pch2;
     pch = strstr(str,"are you smart, are you intelligent, are you dumb");
     if  (pch2 != NULL)
     {
         cout << "What kind of a question is that\n"
              << "I am talking to you aren't I\n"
              << "btw I am smart okay?\n";
              redo = 1;
     }
     
     
     
     //the bottom curly brace is for the do-while loop
     }while (redo == 1);
     
    system("PAUSE");
    return 0;
}

Okay, the problem is that I can't get it to choose one response. Every time I write one of the keywords all of what is in the if loops appears.

I can't get it to single out one response and then go back to the listing one of the responses in string resp.

Any suggestions on the code would be helpful. Thanks in advance.

if (pch1 != NULL)
     {
        cout << "My name, you say\n"
             << "It is Jubajee\n"
             << "Cool eh?\n";
             redo = 1;
     }

Are you talking about the above printing out

My name, you say
It is Jubajee
Cool eh?

all at once?
I'm unclear on what you mean by that.

Where does it go after the output of the phrase if it doesn't loop through one more time?

if (pch1 != NULL)
     {
        cout << "My name, you say\n"
             << "It is Jubajee\n"
             << "Cool eh?\n";
             redo = 1;
     }

Are you talking about the above printing out

My name, you say
It is Jubajee
Cool eh?

all at once?
I'm unclear on what you mean by that.

Where does it go after the output of the phrase if it doesn't loop through one more time?

No i don't mean that. What I mean was that including the response that was supposed to be written out, all the other replies below that were written out as well. Also, when restarted, the loop doesn't restart properly because when I write something again, it shows all the responses.

In all 3 cases you are reading your strstr result into pch rather than pch1 or pch2 in the second and third cases. I think you should declare pch,pch1,pch2 outside of the loop and set them to null at the top of the do/while loop. Since you do not initialize any of the pchs there is a good chance that any one of them is non-null when you check them.

In all 3 cases you are reading your strstr result into pch rather than pch1 or pch2 in the second and third cases. I think you should declare pch,pch1,pch2 outside of the loop and set them to null at the top of the do/while loop. Since you do not initialize any of the pchs there is a good chance that any one of them is non-null when you check them.

Here is the code changed.

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

// a few global variables 

     char str[50]; // this one is for the responses, the 50 is a random number, needed something big
     int redo;     // for restarting the speech thing
     
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()
    {
    
     char * pch = NULL;
     char * pch1 = NULL;
     char * pch2 = NULL;
     char * pch3 = NULL;
    
    
    do
     {
    
     string resp[5] = {
            "Got anything else to say?",
            "That is it, nothing else?",
            "...Anything else",
            "So, what's up?",
            "Talk to me",
            };
     
     // number generator
     
     int which_Num;
     
     unsigned seed = time(0);
     srand(seed);
     
     which_Num = rand() % 5;
     
     cout << resp[which_Num];
     cin.getline (str,50); 
     
     
     pch = strstr (str,"open");
   
     if (pch != NULL)
     {
        open_Something();
     }
     
     pch1 = strstr(str,"what, is, your, name,");
     if (pch1 != NULL)
     {
        cout << "My name, you say\n"
             << "It is Jubajee\n"
             << "Cool eh?\n";
             redo = 1;
     }
     
     pch2 = strstr(str,"are you smart, are you intelligent, are you dumb");
     if  (pch2 != NULL)
     {
         cout << "Wtf, what kind of a question is that\n"
              << "Effing hobo\n"
              << "btw I am smart ookay?\n";
              redo = 1;
     }
     
     pch3 = strstr(str,"bye, goodbye, see you later, cya");
     if  (pch3 != NULL)
     {
         cout << "Goodbye\n"
              << "It was nice talking to you \n";
              return 0;
     }
     
     
     
     //the bottom curly brace is for the do-while loop
     }while (redo == 1);
     
    system("PAUSE");
    return 0;
}

I changed it like you said but now whenever I write something, for some reason it just writes "press any key to continue" and closes. For some reason it doesn't match the users input with any of the keywords even if I write them down.

Never mind everybody, I got it working. Thanks anyways.

I think the problem is that strstr doesn't work the way you think it does. I'm not ultra proficient in the <cstring> functions but I believe it will locate the second argument as a substring of the first argument (and return the position if it was found). You may find that the std::string methods are more useful in this type of application.

Never mind everybody, I got it working. Thanks anyways.

How did you fix it? (I'm just curious)

I'll message you the whole code and how I did it.

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.