0
char* szWords[] = { "caralho", "porra" };
if (IsGoldGrade(pObj)) //Ignore, its ok. //Crashing after i added the
{
    for (int x = 0; x < sizeof(szWords); x++) {
        if (strstr((strlwr((char*)szWords[x])), szChat)) {
            char szBuffer[256]; 
            sprintf(szBuffer, "You can't type %s", szWords[x]);
            Announce(pObj, szBuffer);
            memset(szBuffer, 0, 256);
            return;
        }
    }
}

Idk but I can't use this as "code" on stackoverflow.

Pastebin: http://pastebin.com/u8yit8Rw

PS: I can't use StrStrI because im using Visual Studio 2003.

Grant Birchmeier
  • 17,809
  • 11
  • 63
  • 98
Lcs
  • 19
  • 3
  • 1
    At least tell us what the error is... – takendarkk Dec 02 '16 at 21:10
  • Hello. Im using at server on my game. The error is, my game crashing when this function is executed. – Lcs Dec 02 '16 at 21:12
  • 1
    The file on Pastebin contains words which are probably prohibited by Stack Overflow. Remove those words and you should be able to paste the whole code into your question. – Jack Deeth Dec 02 '16 at 21:12
  • 1
    Yes, and when it crashes it is because of an error. You have to tell us what that error is if you want us to help. – takendarkk Dec 02 '16 at 21:13
  • This looks like a problem which would vanish if you're permitted to replace `char *` with `std::string`. – Jack Deeth Dec 02 '16 at 21:14
  • @takendarkk I can't see the error. My matchserver just crashed. – Lcs Dec 02 '16 at 21:16
  • @JackDeeth thank you for reply. I tried std but I don't know how to use 'for' for strings. Can you make a example for me? – Lcs Dec 02 '16 at 21:17
  • Your debugger will tell you exactly what the error is, and provide a stack trace. – MrEricSir Dec 02 '16 at 22:01
  • Is this a profanity filter? What are you trying to create? And why do you have so many bad words in your code? Lol. – byxor Dec 03 '16 at 02:24
  • @BrandonIbbotson hahaha. This is badwords filter for my game. Thank you for reply. – Lcs Dec 04 '16 at 16:06

2 Answers2

1

Your for loop condition is wrong. You want to iterate the array of pointers to char.
Your loop for (int x = 0; x < sizeof(szWords); x++) continues while x < sizeof(szWords). But sizeof(szWords) is not array length. It just says how many bytes your array occupies in memory. It is system dependant, however it is twice the size of pointer to char, so probably 8 or 16 bytes. You need to divide this size by size of the array element then you will get the proper array size.

Rewrite your for loop like this:

for (int x = 0; x < sizeof(szWords)/sizeof(szWords[0]); x++)

or if your compiler supports C++11 you can try range-based for:

for (const char *word : szWords)

Apart from that, if you are writing C++ code you really should use STL and other C++ features. For instance your array of strings should be declared as:

std::vector<std::string> words = { "caralho", "porra" };

or if your compiler doesnt support C++11 (then really change it...)

std::vector<std::string> words;
words.push_back("caralho");
words.push_back("porra");

for (std::size_t i = 0; i < words.size(); ++i) {
    // since you are using C string functions you will need word as C string
    const char *word = words[i].c_str();
    // do whatever you want with word
}

Also consider reading modern C++ book before writing code.

Rames
  • 918
  • 11
  • 27
1

From the look of it, this is a function which checks if the user has written a prohibited word?

I'd replace char* szWords[]... with std::vector<std::string> to store the prohibited words, and use std::find to see if the input is in that list.

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

std::vector<std::string> bannedWords{"hamster", "elderberries", "etcetera"};

bool isBanned(const std::string &str) {
  return std::find(bannedWords.begin(), bannedWords.end(), str) != bannedWords.end();
}

int main() {
  std::cout << "Is 'wally' banned? " << isBanned("wally") << std::endl;
  std::cout << "Is 'elderberries' banned? " << isBanned("elderberries") << std::endl;
}

More information about std::find is here.

Here's an online demo

Jack Deeth
  • 3,062
  • 3
  • 24
  • 39
  • Thanks my friend. But im using VS 2003 and that doens't allows I use your functions. – Lcs Dec 02 '16 at 21:47
  • @Lcs Ah - I'd overlooked that. Sorry! – Jack Deeth Dec 02 '16 at 21:48
  • @Lcs Try now - `auto` might not be VS 2003-compatible but `string`, `vector` and `find` all should be! – Jack Deeth Dec 02 '16 at 22:00
  • Thanks friend. Do you recommend for me a modern book? I need update myself lol. I even don't know that there is auto – Lcs Dec 02 '16 at 22:06
  • There's two classic books by Scott Meyers: "Effective C++", written in the VS 2003 era and largely applicable today, and "Effective Modern C++", covering what's changed and what's new. There's some great videos on YouTube too, hiding among many very in-depth and opaque videos :) – Jack Deeth Dec 02 '16 at 22:37
  • Oh, thank you. I'll see soon. More one time, thanks :) – Lcs Dec 02 '16 at 22:39