-
AI Chatbox
I'm making this chatbox project called The Not A Friend Project... Anyway, this project is based on a former chat box, hal. I don't remember the creater, he was on this board a while back and posted his creation. Although, he didn't give source code, so I'm starting from scratch. Here's the source code:
Code:
#include <iostream.h>
#include <stdio.h>
#include <fstream.h>
#include <string.h>
#include <conio.h>
char blank[]=" ";
class mainbot
{
public:
mainbot::mainbot();
~mainbot();
int userintf();
int checkinput();
int checkoutput();
private:
char inp[75];
int inpno;
char responce[75];
char inchk[75];
int inchkcnt;
char str;
};
mainbot::mainbot()
{
}
mainbot::~mainbot()
{
}
int mainbot::userintf()
{
cin.getline(inp,75);
checkinput();
checkoutput();
cout << responce;
return 0;
}
int mainbot::checkinput()
{
ifstream input("input.txt");
input >> str;
inchkcnt=1;
while(strcmp(inchk,blank)||!strcmp(inchk,inp))
{
strtok(str, inchkcnt);
inchk=strtok(NULL, inchkcnt);
inchkcnt++;
}
if(!strcmp(inchk,blank))
{
cout << "I don't understand." << endl;
}
else
{
input.close();
checkoutput();
}
}
int mainbot::checkoutput()
{
ifstream output("output.txt");
output >> str;
strtok(str, inchknt);
responce=strtok(NULL, inchknt);
output.close();
}
int main()
{
mainbot interf;
interf.userintf();
return 0;
}
I've gone through debugging for a while bringing 14 errors down to three:
Code:
E:\Programs\Projects\Tests\Chat Bot\notafriend\nofsource.cpp(45) : error C2664: 'strtok' : cannot convert parameter 1 from 'char' to 'char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
E:\Programs\Projects\Tests\Chat Bot\notafriend\nofsource.cpp(46) : error C2664: 'strtok' : cannot convert parameter 2 from 'int' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
E:\Programs\Projects\Tests\Chat Bot\notafriend\nofsource.cpp(63) : error C2065: 'inchknt' : undeclared identifier
They seemed to have either a problem with strtok or with the variables they are taking in. Does anybody know how to solve this? If you see any other problems I may not have picked up, could you post the cures? Thanks.
-
inchknt
i think should be inchkcnt
now u got 2 errors:D
-
..
Salem, does your code work with these adjustments? Because mine doesn't.
-
..
Latest code
Code:
#include <iostream.h>
#include <stdio.h>
#include <fstream.h>
#include <string.h>
#include <conio.h>
char blank[]=" ";
class mainbot
{
public:
mainbot::mainbot();
~mainbot();
int userintf();
int checkinput();
int checkoutput();
private:
char inp[75];
int inpno;
char responce[75];
char inchk[75];
int inchkcnt;
char str[500];
};
mainbot::mainbot()
{
}
mainbot::~mainbot()
{
}
int mainbot::userintf()
{
cin.getline(inp,75);
checkinput();
checkoutput();
cout << responce;
return 0;
}
int mainbot::checkinput()
{
ifstream input("input.txt");
input >> str;
inchkcnt=1;
while(strcmp(inchk,blank)||!strcmp(inchk,inp))
{
strtok(str, inchkcnt);
cout << "hello";
strcpy( inchk, strtok(NULL, inchkcnt) );
inchkcnt++;
}
if(!strcmp(inchk,blank))
{
cout << "I don't understand." << endl;
}
else
{
input.close();
checkoutput();
}
}
int mainbot::checkoutput()
{
ifstream output("output.txt");
output >> str;
strtok(str, inchkcnt);
strcpy( responce, strtok(NULL, inchkcnt) );
output.close();
}
int main()
{
mainbot interf;
interf.userintf();
return 0;
}
Latest Bugs:
Code:
E:\PROGRAMS\Projects\Tests\Chat Bot\notafriend\nofsource.cpp(45) : error C2664: 'strtok' : cannot convert parameter 2 from 'int' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
E:\PROGRAMS\Projects\Tests\Chat Bot\notafriend\nofsource.cpp(46) : error C2664: 'strtok' : cannot convert parameter 2 from 'int' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
E:\PROGRAMS\Projects\Tests\Chat Bot\notafriend\nofsource.cpp(63) : error C2664: 'strtok' : cannot convert parameter 2 from 'int' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
E:\PROGRAMS\Projects\Tests\Chat Bot\notafriend\nofsource.cpp(64) : error C2664: 'strtok' : cannot convert parameter 2 from 'int' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
-
Your compiler is telling you what the problem is. The second parameter to strtok() is expecting a constant character pointer, you are giving it an int!
Example from MSDN...
Code:
/* STRTOK.C: In this program, a loop uses strtok
* to print all the tokens (separated by commas
* or blanks) in the string named "string".
*/
#include <string.h>
#include <stdio.h>
char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;
void main( void )
{
printf( "%s\n\nTokens:\n", string );
/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );
/* Get next token: */
token = strtok( NULL, seps );
}
}
*** EDIT ***
Sorry salem - overlap!
-
..
Ok, I've found an in-program error that I narrowed down to here:
Code:
strcpy( inchk, strtok(NULL, blank) );
Salem, I'm using strtok to scan for anything after the checked number. Shouldn't that work? I couldn't think of another way. :confused:
-
I would say in 9 out of 10 instances strtok is the devil. It isn't thread safe and it modifies your variables. Ick! Try strstr or strchr.
http://www.freebsd.org/cgi/man.cgi?q...SD+5.0-current
http://www.freebsd.org/cgi/man.cgi?q...SD+5.0-current
On a side note, now comlex of an AI are you planning on making? Making a good AI Chatbot is a pretty complex thing. Do you have a plan for parsing input?
Do you mean chatbot or chatbox? And what is a chatbox?