the third parameter to strncmp is the number of characters you want to compare.
As discussed earlier strncmp will only match on complete word matches, so you will not find badwords in phrases.
strstr() is better, but you're probably going to have to write your own comparision routine.
As far as the logic of your latest code...
You can either read all the badwords into an array, and then check the chattext against that array.
(harder because you'll have to dynamically adjust the array size, or at least make the array sufficiently large, and keep a count of how many words you read in)
or you can loop through the file each time and check each line against chattext
(bad if doing over and over again as file i/o is slow)
You currently have a mix of the two methods... with other problems like storing chattext in temp, and then overwriting temp with a line from badwords.txt
Here's an example of looping through the file and comparing each line to chattext
It's based off your latest code
Code:
ifstream file_bwchk ("badwords.txt");
char badword[20];
while(file_bwchk >> badword) {
if(strncmp(chattext, badword, strlen(badword)) == 0) {
cout << "Naughty, naughty" << endl;
break; //found a match no need to keep checking
}
}
an example of storing the words in an array first (these are just to give you an idea... much error checking needed)
Code:
ifstream file_bwchk ("badwords.txt");
char badword[20];
char badwords[20][20];
int badword_cnt = 0;
while(file_bwchk >> badword) {
strcpy(badwords[badword_cnt++], badword);
}
for(int i=0; i<badword_cnt; i++) {
if(strncmp(chattext, badwords[i], strlen(badwords[i])) == 0) {
cout << "Naughty, naughty" << endl;
break;
}
}