cout true for bool(true)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary Wessle

    #1

    cout true for bool(true)

    Hi
    how can I

    bool a(true);
    cout << a;

    and expect it to print out

    true

    and not just

    1


    thanks
  • Phlip

    #2
    Re: cout true for bool(true)

    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!!!


    Comment

    • Sumit Rajan

      #3
      Re: cout true for bool(true)

      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::boola lpha);
      std::cout << a << "\n";

      Regards,
      Sumit.

      Comment

      • Jacek Dziedzic

        #4
        Re: cout true for bool(true)

        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.

        Comment

        • Salt_Peter

          #5
          Re: cout true for bool(true)


          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";

          Comment

          • Victor Bazarov

            #6
            Re: cout true for bool(true)

            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


            Comment

            • Rolf Magnus

              #7
              Re: cout true for bool(true)

              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.

              Comment

              • Markus Moll

                #8
                Re: cout true for bool(true)

                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

                Comment

                • jjds101@yahoo.com

                  #9
                  Re: cout true for bool(true)


                  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?

                  Comment

                  • Markus Moll

                    #10
                    Re: cout true for bool(true)

                    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

                    Comment

                    • Jacek Dziedzic

                      #11
                      Re: cout true for bool(true)

                      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.

                      Comment

                      • Phlip

                        #12
                        Re: cout true for bool(true)

                        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!!!


                        Comment

                        • Markus Moll

                          #13
                          Re: cout true for bool(true)

                          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<nu mpunct_byname<c har(loc))
                          {
                          cout << "Decimal point: " << use_facet<numpu nct_byname<char >
                          >(loc).decimal_ point() << "\n";
                          cout << "Truename: " << use_facet<numpu nct_byname<char >
                          >(loc).truename () << "\n";
                          cout << "Falsename: " << use_facet<numpu nct_byname<char >
                          >(loc).falsenam e() << "\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

                          Comment

                          • Roland Pibinger

                            #14
                            Re: cout true for bool(true)

                            On 2 Nov 2006 09:39:21 -0800, jjds101@yahoo.c om 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

                            Comment

                            • shadowman615@comcast.net

                              #15
                              Re: cout true for bool(true)


                              Roland Pibinger wrote:
                              On 2 Nov 2006 09:39:21 -0800, jjds101@yahoo.c om 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'

                              Comment

                              Working...