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;
}
}
Does the number 0 actually exist? Without getting too philosophical about the meaning of `exist': yes. Mathematically it exists (was introduced) as a neutral element for addition; the defining property of 0 is that 0+a=a for all numbers a.
In ordinary arithmetic, the expression has no meaning, as there is no number which, multiplied by 0, gives a (assuming a≠0), and so division by zero is undefined. Since any number multiplied by zero is zero, the expression 0/0 also has no defined value; when it is the form of a limit, it is an indeterminate form.
So zero divided by zero is undefined. So, let's label it as that. Make sure that when you are faced with something of this nature, where you are dividing by zero make sure you don't put an actual number down, or a variable down.
The short answer is that 0 has no multiplicative inverse, and any attempt to define a real number as the multiplicative inverse of 0 would result in the contradiction 0 = 1.
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"
.
Because you're comparing pointers, not strings. To compare strings either A) just compare the std::string
instead of using c_str()
(best), or B) use strcmp
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With