cout true for bool(true)

Discussion in 'C++' started by Gary Wessle, Nov 2, 2006.

  1. Gary Wessle

    Gary Wessle Guest

    Hi
    how can I

    bool a(true);
    cout << a;

    and expect it to print out

    true

    and not just

    1


    thanks
     
    Gary Wessle, Nov 2, 2006
    #1
    1. Advertising

  2. Gary Wessle

    Phlip Guest

    Gary Wessle wrote:

    > how can I
    >
    > bool a(true);
    > cout << a;
    >
    > and expect it to print out
    >
    > true
    >
    > and not just
    >
    > 1


    Before someone provides a cheaper answer, may I ask if one might imbue a
    locale?

    (Note that some cultures don't call a positive boolean "true" ;-)

    --
    Phlip
    http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
     
    Phlip, Nov 2, 2006
    #2
    1. Advertising

  3. Gary Wessle

    Sumit Rajan Guest

    Gary Wessle wrote:
    > Hi
    > how can I
    >
    > bool a(true);
    > cout << a;
    >
    > and expect it to print out
    >
    > true
    >
    > and not just
    >
    > 1



    Something like this?


    bool a = true;
    std::cout.setf(std::ios::boolalpha);
    std::cout << a << "\n";

    Regards,
    Sumit.
     
    Sumit Rajan, Nov 2, 2006
    #3
  4. Gary Wessle wrote:
    > Hi
    > how can I
    >
    > bool a(true);
    > cout << a;
    >
    > and expect it to print out
    >
    > true
    >
    > and not just
    >
    > 1


    Output the manipulator 'boolapha' to the 'cout' stream
    before outputting your booleans. Also make sure you
    #include <iomanip>.

    HTH,
    - J.
     
    Jacek Dziedzic, Nov 2, 2006
    #4
  5. Gary Wessle

    Salt_Peter Guest

    Gary Wessle wrote:
    > Hi
    > how can I
    >
    > bool a(true);
    > cout << a;
    >
    > and expect it to print out
    >
    > true
    >
    > and not just
    >
    > 1
    >


    The cheap answer Phillip was referring to is:

    std::cout << a ? "true" : "false";
     
    Salt_Peter, Nov 2, 2006
    #5
  6. Salt_Peter wrote:
    > Gary Wessle wrote:
    >> Hi
    >> how can I
    >>
    >> bool a(true);
    >> cout << a;
    >>
    >> and expect it to print out
    >>
    >> true
    >>
    >> and not just
    >>
    >> 1
    >>

    >
    > The cheap answer Phillip was referring to is:
    >
    > std::cout << a ? "true" : "false";


    Should probably be parenthesized:

    std::cout << (a ? "true" : "false");

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 2, 2006
    #6
  7. Gary Wessle

    Rolf Magnus Guest

    Victor Bazarov wrote:

    > Salt_Peter wrote:
    >> Gary Wessle wrote:
    >>> Hi
    >>> how can I
    >>>
    >>> bool a(true);
    >>> cout << a;
    >>>
    >>> and expect it to print out
    >>>
    >>> true
    >>>
    >>> and not just
    >>>
    >>> 1
    >>>

    >>
    >> The cheap answer Phillip was referring to is:
    >>
    >> std::cout << a ? "true" : "false";

    >
    > Should probably be parenthesized:
    >
    > std::cout << (a ? "true" : "false");


    Yup. That's one of those places where operator abuse can stab you in the
    back.

    PS: Yes, using bit shift operators for stream I/O counts as operator abuse
    in my eyes.
     
    Rolf Magnus, Nov 2, 2006
    #7
  8. Gary Wessle

    Markus Moll Guest

    Hi

    Jacek Dziedzic wrote:
    > Output the manipulator 'boolapha' to the 'cout' stream
    > before outputting your booleans. Also make sure you
    > #include <iomanip>.


    Actually, there is no need to #include <iomanip>, as std::boolalpha is
    declared in header <ios>, which must be included by <ostream> (if my
    understanding is correct).

    Cheers
    Markus
     
    Markus Moll, Nov 2, 2006
    #8
  9. Gary Wessle

    Guest

    Rolf Magnus wrote:
    > Victor Bazarov wrote:
    >
    > > std::cout << (a ? "true" : "false");

    >
    > Yup. That's one of those places where operator abuse can stab you in the
    > back.
    >
    > PS: Yes, using bit shift operators for stream I/O counts as operator abuse
    > in my eyes.


    Heh...

    Is it still abuse if you call them insertion/extraction operators?
    What else would one use?
     
    , Nov 2, 2006
    #9
  10. Gary Wessle

    Markus Moll Guest

    Hi

    Phlip wrote:

    > Before someone provides a cheaper answer, may I ask if one might imbue a
    > locale?
    >
    > (Note that some cultures don't call a positive boolean "true" ;-)


    I'm just curious, have you seen an implementation where the output would not
    be "true" and "false" for some locale? I have tried it here, because I
    somehow expected "vrai"/"faux", "juist"/"fout", "wahr"/"falsch" and alike.
    (I was disappointed, it was always "true"/"false")

    Markus
     
    Markus Moll, Nov 2, 2006
    #10
  11. Markus Moll wrote:
    > Hi
    >
    > Jacek Dziedzic wrote:
    >
    >> Output the manipulator 'boolapha' to the 'cout' stream
    >>before outputting your booleans. Also make sure you
    >>#include <iomanip>.

    >
    >
    > Actually, there is no need to #include <iomanip>, as std::boolalpha is
    > declared in header <ios>, which must be included by <ostream> (if my
    > understanding is correct).


    Thanks, I didn't realize that.

    - J.
     
    Jacek Dziedzic, Nov 2, 2006
    #11
  12. Gary Wessle

    Phlip Guest

    Markus Moll wrote:

    > I'm just curious, have you seen an implementation where the output would
    > not
    > be "true" and "false" for some locale? I have tried it here, because I
    > somehow expected "vrai"/"faux", "juist"/"fout", "wahr"/"falsch" and alike.
    > (I was disappointed, it was always "true"/"false")


    Post your code. Maybe "true" and "false" are considered data, not human
    interface, so the locales don't affect them.

    I never studied this topic; I just wanted to learn more about locales.

    --
    Phlip
    http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
     
    Phlip, Nov 3, 2006
    #12
  13. On 2 Nov 2006 09:39:21 -0800, wrote:
    >Rolf Magnus wrote:
    >> Victor Bazarov wrote:
    >>
    >> > std::cout << (a ? "true" : "false");

    >>
    >> Yup. That's one of those places where operator abuse can stab you in the
    >> back.
    >>
    >> PS: Yes, using bit shift operators for stream I/O counts as operator abuse
    >> in my eyes.

    >
    >Heh...
    >
    >Is it still abuse if you call them insertion/extraction operators?
    >What else would one use?


    += or no operator at all (just print())? I agree with Rolf Magnus on
    operator overloading abuse in iostreams.

    Best wishes,
    Roland Pibinger
     
    Roland Pibinger, Nov 3, 2006
    #13
  14. Gary Wessle

    Markus Moll Guest

    Hi

    Phlip wrote:

    > Post your code. Maybe "true" and "false" are considered data, not human
    > interface, so the locales don't affect them.


    --- snip code 1 ---

    #include <iostream>
    #include <ostream>
    #include <locale>

    using namespace std;

    int main()
    {
    locale loc("");
    if(has_facet<numpunct_byname<char> >(loc))
    {
    cout << "Decimal point: " << use_facet<numpunct_byname<char>
    >(loc).decimal_point() << "\n";

    cout << "Truename: " << use_facet<numpunct_byname<char>
    >(loc).truename() << "\n";

    cout << "Falsename: " << use_facet<numpunct_byname<char>
    >(loc).falsename() << "\n";

    }
    }

    --- snip ---

    --- snip code 2 ---

    #include <iostream>
    #include <ostream>
    #include <locale>

    using namespace std;

    int main()
    {
    locale loc("");
    cout.imbue(loc);
    cout << boolalpha << true << " " << false << " " << 1000000 << endl;
    }

    --- snip ---

    Output for code 2 for example was:

    $ LC_ALL=fr_FR ./code2
    true false 1000000
    $ LC_ALL=en_GB ./code2
    true false 1,000,000
    $ LC_ALL=de_DE ./code2
    true false 1.000.000

    Markus
     
    Markus Moll, Nov 3, 2006
    #14
  15. Gary Wessle

    Guest

    Roland Pibinger wrote:
    > On 2 Nov 2006 09:39:21 -0800, wrote:
    > >Rolf Magnus wrote:
    > >> Victor Bazarov wrote:
    > >>
    > >> > std::cout << (a ? "true" : "false");
    > >>
    > >> Yup. That's one of those places where operator abuse can stab you in the
    > >> back.
    > >>
    > >> PS: Yes, using bit shift operators for stream I/O counts as operator abuse
    > >> in my eyes.

    > >
    > >Heh...
    > >
    > >Is it still abuse if you call them insertion/extraction operators?
    > >What else would one use?

    >
    > += or no operator at all (just print())? I agree with Rolf Magnus on
    > operator overloading abuse in iostreams.
    >


    // test1.cpp
    #include <iostream>

    int main() {
    int a = 1;
    std::cout += a;
    return 0;
    }

    > g++ test1.cpp

    test1.cpp: In function `int main()':
    test1.cpp:5: error: no match for 'operator+=' in 'std::cout += a'
     
    , Nov 3, 2006
    #15
  16. Gary Wessle

    Markus Moll Guest

    Hi

    wrote:

    >
    > Roland Pibinger wrote:
    >> += or no operator at all (just print())? I agree with Rolf Magnus on
    >> operator overloading abuse in iostreams.
    >>

    [...]
    >> g++ test1.cpp

    > test1.cpp: In function `int main()':
    > test1.cpp:5: error: no match for 'operator+=' in 'std::cout += a'


    That's because you forgot to #include <ostream>!

    SCNR ;-)
    Markus
     
    Markus Moll, Nov 3, 2006
    #16
  17. Gary Wessle

    Gavin Deane Guest

    Roland Pibinger wrote:
    > On 2 Nov 2006 09:39:21 -0800, wrote:
    > >Rolf Magnus wrote:
    > >> Victor Bazarov wrote:
    > >>
    > >> > std::cout << (a ? "true" : "false");
    > >>
    > >> Yup. That's one of those places where operator abuse can stab you in the
    > >> back.
    > >>
    > >> PS: Yes, using bit shift operators for stream I/O counts as operator abuse
    > >> in my eyes.

    > >
    > >Heh...
    > >
    > >Is it still abuse if you call them insertion/extraction operators?
    > >What else would one use?

    >
    > += or no operator at all (just print())? I agree with Rolf Magnus on
    > operator overloading abuse in iostreams.


    Really? I came straight to C++ without any C background and, as it
    happens, I can hardly remember the last time I bit shifted anything. In
    my eyes, using stream insertion and extraction operators for bit
    shifting counts as operator overloading abuse.

    Gavin Deane
     
    Gavin Deane, Nov 4, 2006
    #17
  18. Gary Wessle

    peter koch Guest

    Roland Pibinger skrev:
    > On 2 Nov 2006 09:39:21 -0800, wrote:
    > >Rolf Magnus wrote:
    > >> Victor Bazarov wrote:
    > >>

    [snip]
    > >>
    > >> PS: Yes, using bit shift operators for stream I/O counts as operator abuse
    > >> in my eyes.

    > >
    > >Heh...
    > >
    > >Is it still abuse if you call them insertion/extraction operators?
    > >What else would one use?

    >
    > += or no operator at all (just print())? I agree with Rolf Magnus on
    > operator overloading abuse in iostreams.
    >
    > Best wishes,
    > Roland Pibinger


    I happen to agree with Deane. For me the "<<" and ">>" operators are
    stream-operators - not bit-shifts, which I use quite rarely. The days
    where you implemented some of the divisions/multiplications by
    bitshifting are long gone, and most other uses of bitshifts are either
    in a library or only for those experienced enough not to be confused
    with operator precedence.

    /Peter
     
    peter koch, Nov 4, 2006
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. abi

    man cout or info cout

    abi, Jun 27, 2003, in forum: C++
    Replies:
    2
    Views:
    1,653
  2. Pmb

    std::cout vs cout

    Pmb, Jun 2, 2004, in forum: C++
    Replies:
    2
    Views:
    4,411
    Leor Zolman
    Jun 2, 2004
  3. Replies:
    4
    Views:
    319
    Howard
    Sep 15, 2005
  4. Stefan Ram

    Re: cout vs std::cout

    Stefan Ram, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    481
  5. bdb112
    Replies:
    45
    Views:
    1,350
    jazbees
    Apr 29, 2009
Loading...

Share This Page