I'm kind of new to C++, so excuse me if this is a little obvious...
I'm having trouble having my program recognize a null value, my code looks like this:
- float a = System::Convert::ToSingle(aBox->Text);
-
if (a == NULL)
-
{
-
float b = System::Convert::ToSingle(bBox->Text);
-
float a = b * 10;
-
}
I've tried several different ways, but this is the most straightforward.
There are no errors, but it fails to recognize the if statement; it stops debugging because of the null value not being corrected.
NULL is a term normally applied to pointers, A NULL pointer is one with the value defined by the system as not pointing to anything, the C/C++ compiler is responsible for converting the value 0 used in a pointer context to the "null" value for the platform that it is compiling for. This is sometimes, but not necessarily the actual value 0.
However you are not using pointers, you have a float and what is more it looks like you are converting from a string of characters to a float. In this context == NULL is equivilent to == 0, testing a float for equivilence is unlikely to work very well because floats (and doubles) only hold approximations to the value. Has it happens in most float representations 0 tends to be an exception, that is there is a exact representation of 0 for a float however the likely hood of achieving that exact representation from a string of characters is unlikely.
The question is are you trying to detect no text in the text box or are you trying to detect a zero (or close to zero) input value.
In the first case you need to test the text directly before converting to a float, get the number of entered characters and check it is non-zero.
In the second you will have to decide what you consider too small and perform a relational comparison, i.e. a < 0.0001