Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > cout true for bool(true)

Reply
Thread Tools

cout true for bool(true)

 
 
Gary Wessle
Guest
Posts: n/a
 
      11-02-2006
Hi
how can I

bool a(true);
cout << a;

and expect it to print out

true

and not just

1


thanks
 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      11-02-2006
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!!!


 
Reply With Quote
 
 
 
 
Sumit Rajan
Guest
Posts: n/a
 
      11-02-2006
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.
 
Reply With Quote
 
Jacek Dziedzic
Guest
Posts: n/a
 
      11-02-2006
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.
 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      11-02-2006

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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-02-2006
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


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      11-02-2006
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.

 
Reply With Quote
 
Markus Moll
Guest
Posts: n/a
 
      11-02-2006
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

 
Reply With Quote
 
jjds101@yahoo.com
Guest
Posts: n/a
 
      11-02-2006

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?

 
Reply With Quote
 
Markus Moll
Guest
Posts: n/a
 
      11-02-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[False,True] and [True,True] --> [True, True]????? bdb112 Python 45 04-29-2009 02:35 AM
Re: cout vs std::cout Stefan Ram C++ 7 09-30-2008 01:55 PM
can cout<<""; can cout<<""; contribute ? wangzhihuii@yahoo.com.cn C++ 4 09-15-2005 06:16 PM
std::cout vs cout Pmb C++ 2 06-02-2004 03:27 PM
man cout or info cout abi C++ 2 06-28-2003 06:42 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57