By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,402 Members | 1,041 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,402 IT Pros & Developers. It's quick & easy.

cout true for bool(true)

P: n/a
Hi
how can I

bool a(true);
cout << a;

and expect it to print out

true

and not just

1
thanks
Nov 2 '06 #1
Share this Question
Share on Google+
17 Replies


P: n/a
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!!!
Nov 2 '06 #2

P: n/a
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.
Nov 2 '06 #3

P: n/a
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.
Nov 2 '06 #4

P: n/a

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

Nov 2 '06 #5

P: n/a
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
Nov 2 '06 #6

P: n/a
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.

Nov 2 '06 #7

P: n/a
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

Nov 2 '06 #8

P: n/a

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 '06 #9

P: n/a
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

Nov 2 '06 #10

P: n/a
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.
Nov 2 '06 #11

P: n/a
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!!!
Nov 3 '06 #12

P: n/a
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

Nov 3 '06 #13

P: n/a
On 2 Nov 2006 09:39:21 -0800, jj*****@yahoo.com 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
Nov 3 '06 #14

P: n/a

Roland Pibinger wrote:
On 2 Nov 2006 09:39:21 -0800, jj*****@yahoo.com 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 '06 #15

P: n/a
Hi

sh**********@comcast.net 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

Nov 3 '06 #16

P: n/a

Roland Pibinger wrote:
On 2 Nov 2006 09:39:21 -0800, jj*****@yahoo.com 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

Nov 4 '06 #17

P: n/a

Roland Pibinger skrev:
On 2 Nov 2006 09:39:21 -0800, jj*****@yahoo.com 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

Nov 4 '06 #18

This discussion thread is closed

Replies have been disabled for this discussion.