# Can a float be zero?

• 03-21-2006
derek tims
Can a float be zero?
basically, in my program, i want to enter ANY number, including 0. if i enter anything other than a number, it will ask to re-enter.

BUT, when i enter 0, it asks me to re-enter, yet 0 should be valid.

is there a way of saying

make sure the entry is a number, and allow 0 to be a value if entered.

Code:

``` cout << "\n\nPlease input a, the lower bound:  ";     cin >> a;             while (!(a) || cin.get() != '\n' )    // checks for bad input of a         {         cin.clear();         cin.ignore(1000, '\n');         textcolor (12);         cprintf ("\nYou have entered the data incorrectly.\n");                        cout << "\nPlease input a, the lower bound: ";         cin >> a;                         }```
is 0 valid for a float?
• 03-21-2006
laserlight
If a = 0, what does !(a) evaluate to?
• 03-21-2006
grumpy
0 is a valid value for a float.

Your problem is the condition in your while() clause. If a is a float, the test !a (or !(a)) evaluates to true if a is zero.

You might want to check up on what "cin >> a" does if non-numeric data is encountered on the stream. The result is setting an error status on the stream, not setting a to be any particular value.
• 03-21-2006
SlyMaelstrom
Code:

`!(a)`
Do you understand what this says?

You should as yourself what is the difference between 0 and non-zero numbers as far as boolean is concerned?

EDIT: Next time... I hit submit... then I put milk and sugar in my coffee.
• 03-21-2006
derek tims
!(a) means not equal to a.

do u mean that for boolean, if false it is always 0, and if true always 1?

so from what i gather, by setting a = 0, it will appear to be false.
• 03-21-2006
Ancient Dragon
>>!(a) means not equal to a.
no it doesn't mean that.

consider this
Code:

```int a = 0; if( !a ) {   cout << "a == 0\n"; <<< This will be printed } else {   cout << "a != 0\n"; }```
So you can rewrite the loop like this (which is obviously not what you intend)
Code:

` while (a == 0 || cin.get() != '\n' )`
• 03-21-2006
derek tims
thanks for the help!!

here's what i've come up with, and it seems to work.

Code:

``` cin >> a;     z = a;        // setting z = a, and so can later allow an input of zero.                 // without this, it would fail, but zero is an allowed number          while ( (!a && z != 0) || cin.get() != '\n' )                    // checks for bad input of a, and allows for a to be zero         {         cin.clear();         cin.ignore(1000, '\n');         textcolor (12);         cprintf ("\nYou have entered the data incorrectly.\n");                        cout << "\nPlease input a, the lower bound: ";         cin >> a;         z = a;            }```
• 03-21-2006
OnionKnight
Code:

`(!a && z != 0)`
This will always evaluate to false since it's equal to (a == 0 && a != 0) which is (!a && a).
• 03-21-2006
derek tims
it works.........

no matter what i do, it seems to work fine.

type 0 in starihgt away, works fine.

type in any garbage in, press enter, get the please re-enter, enter 0, and it works.
• 03-21-2006
user_231
the problem is ,it works with only with a 0.
• 03-21-2006
Daved
What happened to while (!(cin >> a) || cin.get() != '\n') ? The code I gave you last time should work the same for ints or floats without any changes. It is the return value of cin >> a that is true or false that determines whether a float was read in or not.

If you explain your thinking behind removing the cin >> from the while control maybe we can help figure out how to do what you are trying to accomplish.
• 03-21-2006
7stud
Quote:

do u mean that for boolean, if false it is always 0, and if true always 1?
0 evaluates to false, and any non-zero number(e.g. -10, 3) evaluates to true.
• 03-21-2006
grumpy
Quote:

Originally Posted by SlyMaelstrom
EDIT: Next time... I hit submit... then I put milk and sugar in my coffee.

You have it good. Some days, I can hit "submit", then go downstairs and make a cup of coffee......
• 03-21-2006
derek tims
daved: basically, the code i was given did work ok. but i sometimes needed to press ENTER more than once to go to the next step. so fiddling around with it i tried a few combinations, and what i have put seemed to work.