473,569 Members | 2,438 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(Obje ct* 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 2757
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(Obje ct* 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(Obje ct* 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(Obje ct* 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(Obje ct* 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(Obje ct* 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(Obje ct* 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(Obje ct* 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(Obje ct* 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.comwro te in message
news:11******** *************@k 70g2000cwa.goog legroups.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(Obje ct* 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(Obje ct* 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.comwro te in message
news:11******** *************@k 70g2000cwa.goog legroups.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(Obje ct* 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

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

Similar topics

9
5477
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. I'm only adding it to the tree node, where each custom tree node is then added to a normal tree control I'm trying to get an add-in to interact with...
0
1915
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.. Chris. code :
9
10876
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 the wheel, right? Everything so far is working well with the Active Directory. The problem I am having is with adding File Permissions to a...
3
4863
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 the best method? Do you have a sample of how to do this?
6
1938
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 a image. Does someone got an example or somewhere I can read more about it except the MSDN? thx in advance.
6
3361
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 namespace for my component is automatically added to the using directive of the codebehind. And, the appropriate assembly is added to the "References" for...
1
4220
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 the example to work. I created the following two classes, provided with the example: *-*-**-*-*-*-*-*-*-*-*-*-**-*-*-*-*-CheckBoxColumn...
2
7528
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> <check>1</check>
10
2187
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 has string value. I really do not understand why push_back() function is trying to remove previously inserted data. Thanks for any help
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5223
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3657
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
946
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.