Why isn't "0" == "0"?

c++, stringstream, winapi

Solution

Comparing C-style strings with `==` means "Do the first elements of these strings have the same address?". It doesn't actually compare the contents of the strings. For that, you need `strcmp`.

However, you have no reason to compare C-style strings - just use the `std::string` returned from `str()`, which can be compared using `==`, like so: `input.str() != "0"`.

Problem

I do not have UNICODE in this project. This is a WinAPI project and the variable "input" is a stringstream with the default value "0". Why does the first id statement run and not 2nd one even though the string itself IS "0"? ``` void calc::AddToInput(int number) { MessageBox(NULL, input.str().c_str(), "Input", NULL); //Shows "0" if(input.str().c_str() != "0") //Runs even though it shouldn't { input << number; } else if(input.str().c_str() == "0") //Doesn't run though it should { input.str(""); input << number; } } ```

Original source