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.