473,507 Members | 8,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

adding a bool to a string

Hi All,

I'm trying to print boolean values and after I searched the entire web,
I couldn't find anything that does this =/ The code that I ended up
using is as follows:

string boolString(Object* obj_, size_t maxOutput_){
string string_;

for (size_t i=0; i<maxOutput_; i++){

if( sizeof(obj_->boolean(i)) == sizeof(bool) ){
if (obj_->boolean(i) == true)
string_ += "1";
else
string_ += "0";
}
}

return string_;
}

Is there any way to simplify this code?

Nov 15 '06 #1
12 2749
aaragon wrote:
Hi All,

I'm trying to print boolean values and after I searched the entire web,
I couldn't find anything that does this =/ The code that I ended up using
is as follows:

string boolString(Object* obj_, size_t maxOutput_){
string string_;

for (size_t i=0; i<maxOutput_; i++){

if( sizeof(obj_->boolean(i)) == sizeof(bool) ){
What is the above line supposed to do? What does obj_->boolean(i) return?
Note that sizeof is always computed at compile time.
if (obj_->boolean(i) == true)
string_ += "1";
else
string_ += "0";
}
}

return string_;
}

Is there any way to simplify this code?
string boolString(Object* obj_, size_t maxOutput_){
stringstream stream_;

for (size_t i=0; i<maxOutput_; i++)
// depending on the return type of boolean(), you can remove the cast
stream_ << bool(obj_->boolean(i));

return stream_.str();
}

Nov 15 '06 #2
aaragon wrote:
I'm trying to print boolean values and after I searched the entire
web, I couldn't find anything that does this =/ The code that I
ended up using is as follows:

string boolString(Object* obj_, size_t maxOutput_){
string string_;

for (size_t i=0; i<maxOutput_; i++){

if( sizeof(obj_->boolean(i)) == sizeof(bool) ){
Why do you need to check the size? What does it give you?
if (obj_->boolean(i) == true)
string_ += "1";
else
string_ += "0";
This could be written

string_ += (obj_->boolean(i) ? '1' : '0');

which I find a bit simpler.

Also, consider that you call 'boolean' member twice here (once to
check the size and the other to actually get the value); if the
function has side effects, calling it twice may not be what you
want.
}
}

return string_;
}

Is there any way to simplify this code?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 15 '06 #3

Victor Bazarov wrote:
aaragon wrote:
I'm trying to print boolean values and after I searched the entire
web, I couldn't find anything that does this =/ The code that I
ended up using is as follows:

string boolString(Object* obj_, size_t maxOutput_){
string string_;

for (size_t i=0; i<maxOutput_; i++){

if( sizeof(obj_->boolean(i)) == sizeof(bool) ){

Why do you need to check the size? What does it give you?
I need to check the size because the function may not return a bool but
an integer for example.
>
if (obj_->boolean(i) == true)
string_ += "1";
else
string_ += "0";

This could be written

string_ += (obj_->boolean(i) ? '1' : '0');

which I find a bit simpler.

Also, consider that you call 'boolean' member twice here (once to
check the size and the other to actually get the value); if the
function has side effects, calling it twice may not be what you
want.
}
}

return string_;
}

Is there any way to simplify this code?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 15 '06 #4

Rolf Magnus wrote:
aaragon wrote:
Hi All,

I'm trying to print boolean values and after I searched the entire web,
I couldn't find anything that does this =/ The code that I ended up using
is as follows:

string boolString(Object* obj_, size_t maxOutput_){
string string_;

for (size_t i=0; i<maxOutput_; i++){

if( sizeof(obj_->boolean(i)) == sizeof(bool) ){

What is the above line supposed to do? What does obj_->boolean(i) return?
Note that sizeof is always computed at compile time.
if (obj_->boolean(i) == true)
string_ += "1";
else
string_ += "0";
}
}

return string_;
}

Is there any way to simplify this code?

string boolString(Object* obj_, size_t maxOutput_){
stringstream stream_;

for (size_t i=0; i<maxOutput_; i++)
// depending on the return type of boolean(), you can remove the cast
stream_ << bool(obj_->boolean(i));

return stream_.str();
}
Sometimes boolean() may return an integer, so doing a cast to a boolean
is not a good idea. This is the reason behind the check of the size of
whatever type boolean() returns. I thought that the sream can
automatically convert to 1 or 0 from a boolean as

stream_ += boolean(i);

but this is not the case. However, if I repeat the previous with an
integer, it works fine.

Nov 15 '06 #5
Rolf Magnus wrote:
aaragon wrote:
>Hi All,

I'm trying to print boolean values and after I searched the entire
web,
I couldn't find anything that does this =/ The code that I ended up
using is as follows:

string boolString(Object* obj_, size_t maxOutput_){
string string_;

for (size_t i=0; i<maxOutput_; i++){

if( sizeof(obj_->boolean(i)) == sizeof(bool) ){

What is the above line supposed to do? What does obj_->boolean(i)
return? Note that sizeof is always computed at compile time.
> if (obj_->boolean(i) == true)
string_ += "1";
else
string_ += "0";
}
}

return string_;
}

Is there any way to simplify this code?

string boolString(Object* obj_, size_t maxOutput_){
stringstream stream_;

for (size_t i=0; i<maxOutput_; i++)
// depending on the return type of boolean(), you can remove the
cast stream_ << bool(obj_->boolean(i));

return stream_.str();
}
Consider that some locales do not have '1' or '0' as the default
for outputting a bool value. If the OP wanted '1' or '0', it should
probably be explicit.

And how is using a stringstream simpler (better) than, say,

for (size_t i=0; i<maxOutput_; i++)
string_ += (bool(obj_->boolean(i)) ? '1' : '0');

return string_;

?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 15 '06 #6

"aaragon" <al**************@gmail.comwrote in message
news:11*********************@k70g2000cwa.googlegro ups.com...
>
Victor Bazarov wrote:
>aaragon wrote:
I'm trying to print boolean values and after I searched the entire
web, I couldn't find anything that does this =/ The code that I
ended up using is as follows:

string boolString(Object* obj_, size_t maxOutput_){
string string_;

for (size_t i=0; i<maxOutput_; i++){

if( sizeof(obj_->boolean(i)) == sizeof(bool) ){

Why do you need to check the size? What does it give you?

I need to check the size because the function may not return a bool but
an integer for example.
And how do you know that a value is a bool just because it's the same size
as one?

Also, boolean() is your own function, so you should already know what its
return type is. It can't sometimes return a bool and other times return an
int. (Unless, I guess, if "boolean" isn't really a function member of obj_
at all, but is instead a function pointer which you assign elsewhere.)

-Howard

Nov 15 '06 #7
aaragon wrote:
Victor Bazarov wrote:
>aaragon wrote:
>>I'm trying to print boolean values and after I searched the entire
web, I couldn't find anything that does this =/ The code that I
ended up using is as follows:

string boolString(Object* obj_, size_t maxOutput_){
string string_;

for (size_t i=0; i<maxOutput_; i++){

if( sizeof(obj_->boolean(i)) == sizeof(bool) ){

Why do you need to check the size? What does it give you?

I need to check the size because the function may not return a bool
but an integer for example.
So? An integer is convertible to bool. Besides, sizes of 'bool' and
'int' _can_ be the same. The check is no good. You need to check
'typeid' to be sure.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 15 '06 #8
>
Consider that some locales do not have '1' or '0' as the default
for outputting a bool value. If the OP wanted '1' or '0', it should
probably be explicit.

And how is using a stringstream simpler (better) than, say,

for (size_t i=0; i<maxOutput_; i++)
string_ += (bool(obj_->boolean(i)) ? '1' : '0');

return string_;

?
Well, this definitely will work for booleans. But, the object I'm
working on, would also be a container for other values (say double,
integers) in which case boolean representation is no longer useful
(forget about the name boolean(i), it could be anything, like
value(i)). Therefore, the cast that you're using is not useful
anymore.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 15 '06 #9
Howard wrote:
"aaragon" <al**************@gmail.comwrote in message
news:11*********************@k70g2000cwa.googlegro ups.com...
>>
Victor Bazarov wrote:
>>aaragon wrote:
I'm trying to print boolean values and after I searched the entire
web, I couldn't find anything that does this =/ The code that I
ended up using is as follows:

string boolString(Object* obj_, size_t maxOutput_){
string string_;

for (size_t i=0; i<maxOutput_; i++){

if( sizeof(obj_->boolean(i)) == sizeof(bool) ){

Why do you need to check the size? What does it give you?

I need to check the size because the function may not return a bool
but an integer for example.

And how do you know that a value is a bool just because it's the same
size as one?

Also, boolean() is your own function, so you should already know what
its return type is. It can't sometimes return a bool and other times
return an int. (Unless, I guess, if "boolean" isn't really a
function member of obj_ at all, but is instead a function pointer
which you assign elsewhere.)
If it's a function pointer, the return value type is part of the type
of that pointer. C++ is strict in that regard. You cannot have return
value types change arbitrarily during run-time.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 15 '06 #10
aaragon wrote:
>Consider that some locales do not have '1' or '0' as the default
for outputting a bool value. If the OP wanted '1' or '0', it should
probably be explicit.

And how is using a stringstream simpler (better) than, say,

for (size_t i=0; i<maxOutput_; i++)
string_ += (bool(obj_->boolean(i)) ? '1' : '0');

return string_;

?

Well, this definitely will work for booleans. But, the object I'm
working on, would also be a container for other values (say double,
integers) in which case boolean representation is no longer useful
(forget about the name boolean(i), it could be anything, like
value(i)). Therefore, the cast that you're using is not useful
anymore.
If for some twisted reason the 'boolean' function returns an 'int'
and not a 'bool', it shouldn't be used here, which means you need
to employ some kind of templated mechanism for rejecting classes
with 'boolean' members returning anything except 'bool'. See your
favourite C++ book on how to specialise your function on that.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 15 '06 #11

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:ej**********@news.datemas.de...
>Also, boolean() is your own function, so you should already know what
its return type is. It can't sometimes return a bool and other times
return an int. (Unless, I guess, if "boolean" isn't really a
function member of obj_ at all, but is instead a function pointer
which you assign elsewhere.)

If it's a function pointer, the return value type is part of the type
of that pointer. C++ is strict in that regard. You cannot have return
value types change arbitrarily during run-time.
Oh, right, of course. Thanks.

-Howard
Nov 15 '06 #12
aaragon wrote:
>What is the above line supposed to do? What does obj_->boolean(i) return?
Note that sizeof is always computed at compile time.
if (obj_->boolean(i) == true)
string_ += "1";
else
string_ += "0";
}
}

return string_;
}

Is there any way to simplify this code?

string boolString(Object* obj_, size_t maxOutput_){
stringstream stream_;

for (size_t i=0; i<maxOutput_; i++)
// depending on the return type of boolean(), you can remove the cast
stream_ << bool(obj_->boolean(i));

return stream_.str();
}

Sometimes boolean() may return an integer, so doing a cast to a boolean
is not a good idea.
Could you elaborate on how you think you did that? It's not possible. A
function's return type is fixed at compile time. Somewhere, you declared
the function, and part of that declaration is the return type. So if
boolean() returns a bool, it will always do that.
This is the reason behind the check of the size of whatever type boolean()
returns.
The size of bool depends on the implementation. Most often, it's the same as
char or the same as int.
I thought that the sream can automatically convert to 1 or 0 from a
boolean as

stream_ += boolean(i);
You need operator <<, not += to put something into a stream.

Nov 15 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
5472
by: James Geurts | last post by:
Hey all... I posted this in the vs.net ide group too, but people are not answering, so I figured that it might be more appropriate here. I'm not sure if I'm adding a designer to my code properly. ...
0
1908
by: Chris Millar | last post by:
I have a user control that i wish to extend to change the date when the user selects the numeric up down button. The code explains itself, hope someone can help. any ideas appreaciated.. ...
9
10859
by: Ben Dewey | last post by:
Project: ---------------------------- I am creating a HTTPS File Transfer App using ASP.NET and C#. I am utilizing ActiveDirectory and windows security to manage the permissions. Why reinvent...
3
4856
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
6
1931
by: Richard | last post by:
On my webpage I am making I want that if users view any image in my gallery a small logo/tekst will appear in the image. I know it's possible but I haven't got a clue how to get a logo or tekst on...
6
3357
by: cpnet | last post by:
I've authored a custom web component (a non-ui component kinda like a DataSet) and so far it's working. When my web component is added to the web form in the designer from the toolbox, the...
1
4213
by: sianan | last post by:
I tried to use the following example, to add a checkbox column to a DataGrid in an ASP.NET application: http://www.codeproject.com/aspnet/datagridcheckbox.asp For some reason, I simply CAN'T get...
2
7522
by: Joe | last post by:
Anyone can suggest the best method of reading XML and adding data to ListView? Here is the xml data structure:: <xml> <site> <url>http://www.yahoo.com</url> <lastupdate></lastupdate>...
10
2181
by: oktayarslan | last post by:
Hi all; I have a problem when inserting an element to a vector. All I want is reading some data from a file and putting them into a vector. But the program is crashing after pushing a data which...
0
7221
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7109
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7481
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3190
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.