\0 is a null character, that's so that it stops trying to convert the string to lower case once it reaches the end of the string.
This is a discussion on simple string, search in input within the C++ Programming forums, part of the General Programming Boards category;
\0 is a null character, that's so that it stops trying to convert the string to lower case once it reaches the end of the string.
thanks dweia!Originally Posted by Dweia
Is there anyone who help can me further with this problem?
The while for checking for badwords, I now got this part:
This one gives me this error:Code:char badwords[20][20]; ifstream file_bwchk ("badwords.txt"); char temp[20]; strcpy (temp, chattext); int i = 0; cout << temp << endl; while(file_bwchk >> temp) { for(i = 0; i < 20; ++i) { if(strncmp(temp, badwords[i]) == 0) { cout << "Naughty, naughty" << endl; } } }
I guess I have to use that "unsigned int" somewhere, but I can't exactly figure out where.Code:/usr/include/string.h:100: error: too few arguments to function `int strncmp(const char*, const char*, unsigned int)'
When just putting an int into there without actully doing something with it the console returns "Naughty, naughty" all the time, also when saying "test" or something like that. And it does say it twenty times to, not one time
It looks like it is reconizing every line of the badwords.txt (which excists of 3 lines or so, not twenty) with the chattext.
Does anyone know how I have to use that extra int?
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
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]; while(file_bwchk >> badword) { if(strncmp(chattext, badword, strlen(badword)) == 0) { cout << "Naughty, naughty" << endl; break; //found a match no need to keep checking } }
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; } }
Last edited by spydoor; 08-09-2005 at 12:17 PM.