Can a float be zero?

This is a discussion on Can a float be zero? within the C++ Programming forums, part of the General Programming Boards category; basically, in my program, i want to enter ANY number, including 0. if i enter anything other than a number, ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    24

    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?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,787
    If a = 0, what does !(a) evaluate to?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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.
    Sent from my iPad«

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    24
    !(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.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    >>!(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' )
    Last edited by Ancient Dragon; 03-21-2006 at 05:35 AM.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    24
    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;     
            }

  8. #8
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Code:
    (!a && z != 0)
    This will always evaluate to false since it's equal to (a == 0 && a != 0) which is (!a && a).

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    24
    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.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    3
    the problem is ,it works with only with a 0.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,348
    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.

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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......

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    24
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21