CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    26

    problem with boolean

    Hi, I wrote this code but why writing 0 it print true instead of false?
    Thanks!

    #include <iostream>
    int main ()
    {
    bool a;
    std::cout << "insert 0 or 1:";
    std::cin >> a;
    if (a=true)
    {
    std::cout << "a is true";
    }
    else
    {
    std::cout << "a is false";
    }

    }

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: problem with boolean

    Code:
    if (a=true) // should be ==

  3. #3
    Join Date
    Apr 2008
    Posts
    725

    Re: problem with boolean

    welcome to c++ gotchas 101

  4. #4
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    26

    Re: problem with boolean

    thanks now it works
    if a number bigger then 1 is true, why writing for example 8 it print false?

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: problem with boolean

    Quote Originally Posted by abc++ View Post
    if a number bigger then 1 is true, why writing for example 8 it print false?
    Any value other than 0 is convertible to true.

  6. #6
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    26

    Re: problem with boolean

    but it print false...

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: problem with boolean

    Quote Originally Posted by abc++ View Post
    but it print false...
    Let's see your updated program.

    And please use code tags when posting code.

    Regards,

    Paul McKenzie

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: problem with boolean

    See this FAQ. Int a nutshell, don't try to assign int values to a bool. Acceptable values are true and false.

    http://www.codeguru.com/forum/showthread.php?t=332831

  9. #9
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    26

    Re: problem with boolean

    Excuse me I don't see the code button, however my updated code is this and typing a number bigger then 1 it print false:

    Code:
    #include <iostream>
    int main ()
    {
    bool a;
    std::cout << "insert 0 or 1:";
    std::cin >> a;
    if (a==true)
    {
    std::cout << "a is true";
    }
    else
    {
    std::cout << "a is false";
    }
    }

  10. #10
    Join Date
    Aug 2009
    Location
    Romania->Felnac
    Posts
    48

    Re: problem with boolean

    An bool in c++ is 1 byte (8 bits) if you store the value 7 in that byte and compare it with the value 1 (true) it will go on the false branch (because those values are not equal) even if they both evaluate to true.

    You can modify the code like this and then you will get the desired behavior:
    Code:
    //...
    if(a)
    //...

  11. #11
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    26

    Re: problem with boolean

    in input a bool must be only 0 or 1 ?

    how can i write a text like "Hello \n" but considering \n a normal text?

  12. #12
    Join Date
    Aug 2009
    Location
    Romania->Felnac
    Posts
    48

    Re: problem with boolean

    No, that is what i tried to explain to you, a bool can store other values, since it is not a single bit (it's 8)

    You need to double the \ to appear:
    Code:
    cout << "Hello \\n";

  13. #13
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: problem with boolean

    Quote Originally Posted by Zlatomir View Post
    No, that is what i tried to explain to you, a bool can store other values, since it is not a single bit (it's 8)

    You need to double the \ to appear:
    Code:
    cout << "Hello \\n";
    It can, but it shouldn't. true and false are the two documented values you can assign it.

  14. #14
    Join Date
    Aug 2009
    Location
    Romania->Felnac
    Posts
    48

    Re: problem with boolean

    @GCDEF agree with you that it shouldn't, but it can and there can be some very tricky situations regarding this fact.
    I talk about for example uninitialized bool which can return false if compared to true and again false if compared to false, so the programmer should know that the bool can store 8 bits not just 1.

    //my bad that i didn't specifically said that it's not recommended to use bool for storing anything else than true or false.

  15. #15
    Join Date
    Apr 1999
    Posts
    27,449

    Re: problem with boolean

    Quote Originally Posted by abc++ View Post
    in input a bool must be only 0 or 1 ?
    What is the purpose of having a bool as input if you want to store more values? Why not just make it an int instead of a bool?
    Code:
    #include <iostream>
    int main ()
    {
       bool a;
       int value;
       std::cout << "insert 0 or 1:";
       std::cin >> value;
       a = (value?true:false);
    }
    Now there is no issue. Since you want 0 to mean false, and anything else to mean true, then this is guaranteed to work correctly.

    Regards,

    Paul McKenzie

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured