1

Using C++ MFC and Visual Studio 2008.

Sorry for the newbie question, but I'm not sure why this isn't working. I'm making a function that finds the standard illegal characters for XML in a char * that's passed to it, but I get a strange error.

char* XMLIllegalCharacterParser(char *input){
    char *Temp;
    for (size_t i = 0; i < strlen(input); i++){
        if(input[i] == "\"" || input[i] == "\'" || input[i] == "&")
    }
    return Temp;
}

1>.\FileImport.cpp(868) : error C2446: '==' : no conversion from 'const char *' to 'int'

I can't for the life of me figure out why it isn't working. What syntax do I have wrong?

PS: I know that I only have 3 of the characters there. I got this error and I wanna fix it before I add the other two.

Here my after code:

char* XMLIllegalCharacterParser(char *input){
char *Temp;
for (size_t i = 0; i < strlen(input); i++){
    switch(input[i]){
        case '\"':
            strcat_s(Temp, 6, "&quot;");
            break;
        case '\'':
            strcat_s(Temp, 6, "&apos;");
            break;
        case '&':
            strcat_s(Temp, 5, "&amp;");
            break;
        case '<':
            strcat_s(Temp, 4, "&lt;");
            break;
        case '>':
            strcat_s(Temp, 4, "&gt;");
            break;
        default:
            strcat_s(Temp, 1, (const char*)input[i]);
            break;
    }
}
return Temp;
}
user3215251
  • 239
  • 1
  • 16
  • Don't take a non-const `char *` when you don't modify it. That just limits how people can call your function (which is, imo, very frustrating). Anyway, your `if` statement has no body and since this is C++, use `std::string`. – chris Jun 27 '14 at 12:29
  • 1
    Also, when doing single character comparison use the single quote `'`, like `input[i] == '&'` – emartel Jun 27 '14 at 12:30
  • Yeah, I didn't include the body just yet. Now that I have the solution to the error, I'll have a proper body soon. – user3215251 Jun 27 '14 at 12:37
  • maybe this will help: http://stackoverflow.com/questions/20303100/no-conversion-from-const-char-to-int – iedoc Jun 27 '14 at 12:39

2 Answers2

5

You probably mean single quotes instead of double:

input[i] == '"'
            ^ ^

Same goes for the other cases. When you use double quotes, as in "&", you create a "string literal". But input[i] is a character: you can't compare a string literal with a character.

What's more, even if input[i] would also be a string literal, that wouldn't be the right way to compare c-style strings in C++.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • 1
    This is a correct answer. It would be a good answer if it also included the *why.* – Angew is no longer proud of SO Jun 27 '14 at 12:31
  • Thank you! This solved it right up. The function is a first draft and will be expanded soon, but that solved my major issue. – user3215251 Jun 27 '14 at 12:36
  • I would argue `==` is the right to compare strings in C++. You just have to use the right type for your string instead of making life harder for yourself. – chris Jun 27 '14 at 12:36
  • @chris I made explicit that I meant c-style strings. – cnicutar Jun 27 '14 at 12:39
  • @chris: Curious thing C++ people cannot remember the namespace of their string class and insist on decorating the name of the inherited and commonly used one instead. Still, somehow beside the point imho. – Deduplicator Jun 27 '14 at 12:40
  • @Deduplicator That's because in proper C++, C-style strings are not commonly, but **very rarely** used. You don't want to touch a nul-terminated `char` array unless you're dealing with a C API. – Angew is no longer proud of SO Jun 27 '14 at 12:58
  • @Angew Or you need a string literal of any persuasion (narrrow, wide, utf8, utf16, utf32, what-have-you). Like, for initializing a C++ `std::string`. So, all the time... – Deduplicator Jun 27 '14 at 12:59
  • @Deduplicator I could have been a bit more explicit - you don't want to touch a string explicitly referred to by a `const char*` or `char*`. – Angew is no longer proud of SO Jun 27 '14 at 13:14
3

You're comparing a char to strings. Change this line:

if (input[i] == '"' || input[i] == '\'' || input[i] == '&')
Tom Zych
  • 13,329
  • 9
  • 36
  • 53