0

I'm fairly new to C++ and have run into a problem when trying to compare two chars, here is an example:

#define PartOne "He"
#define PartTwo "llo"
char Final1Var[] = PartOne PartTwo;
char ComapreVars[] = "Hello";

if(Final1Var == ComapreVars)//This is were the problem occurs, the chars are supposed to be equal to each other BUT for some reason the 'if' statement ends up determining they're not?
   InGameDialog::Alert("They Match");
else
    InGameDialog::Alert("They Don't Match");

What is going wrong with the code? I can't imagine why this wouldn't work? Any suggestions?

C0d1ng
  • 441
  • 6
  • 17

2 Answers2

-1

in c++ character array's could not be compared using == operator you have to use strcmp function or string comparison function.

-1

This is a very common question. I'll mark it as duplicate once I find a good answer to this among the other questions.

In the mean time, you're not comparing chars, you're comparing char[]s, which is entirely different. You'll want to use strcmp, strncmp, or std::string types would be an even better solution.

What is array decaying? has some reasonable explanations for what's going on in your code and why.

Community
  • 1
  • 1
Jfevold
  • 422
  • 2
  • 11