Archived

This topic is now archived and is closed to further replies.

need help with this bool function..

This topic is 5022 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

alright, i''m having a problem. there''s a function that returns a bool value. i''m defining another function that uses the result from this 1st bool function. //say first bool function name is first_function //2nd function is bool second bool something::second ( string blah ) { //so, i''m calling the first function below int x; x = first_function(blah); if (x == 0) { //do whatever } else if (x != 0) { //do whatever } //and so on } when i run the program, i''m getting ''segmentation fault''. i''m not too experienced, but it seems like there may be something wrong with me storing a bool value in int x.... is there? any other way i can use the results? any help is appreciated.... thanks.

Share this post


Link to post
Share on other sites
Hey red-dragonX,

I''m not totally sure what the problem would be. My best bet is just change "int x" ot "bool x", the rest of the code should work out fine I think.

If not can you please explain in more detail.

- [BDS]StackOverflow

Share this post


Link to post
Share on other sites
If you''re working with only bool values, you can use a boolean variable:

bool something::second ( string blah )
{
//so, i''m calling the first function below

bool x = first_function(blah);
if (x == true)
{
//do whatever

}
else
{
//do whatever

}
//and so on

}


or you could do this, if it''s not above your head:


bool something::second ( string blah )
{
if(first_function(blah)) //If first_function(blah) = 1 then do this

{

}
else //or else go here

{

}
}


By default, any non-zero is considered "true." So you could do
 if ( x )  
which is equivalent to
 if ( x == true) 


Hope that helped

Share this post


Link to post
Share on other sites
For instance here is how I would write my program:

bool something::second( string blah ) {
bool success = first_function( blah );

if( success ) {
// Do whatever

}else {
// Do whatever

}
}


Edit: Kaezin's example is very good too.


I hope this makes sense,
- [BDS]StackOverflow

[edited by - BlueDev on March 18, 2004 11:21:51 PM]

Share this post


Link to post
Share on other sites
thanks both of you. i think something was wrong with my test file, kuz i changed it and now, no segmentation fault.
so if i say, if !(first_function(blah))
that would mean it''s false, correct?

Share this post


Link to post
Share on other sites
Correct red-dragonX,

Since you are dealing with bools you can either do:

if( !(first_function(blah)) )
or
if( first_function(blah) == false )

Though the first one always looks more readable.

Edit, Note: bools only operate as 1 (success) or 0 (failed)

So you could do something like, "bool myBool = true" and then check if its true by doing something like:

if( myBool ) // because when doing an if statement and not defining what the constant equals it automatically chooses 1 or

if( !myBool ) // that will do the opposite of 1, which is 0


Hope this all makes sense now,
- [BDS]StackOverflow

[edited by - BlueDev on March 18, 2004 11:35:48 PM]

Share this post


Link to post
Share on other sites
thanks everyone, but i'm having another problem. in our test file, there are two strings, say,
thissucks
thissucsksdfjjsdfljslfdjlsdf

see how the s and k are switched in the beginning of the second string? well when i am to compare the first one to the second one, i'm suppose to get a true value. so in this function, i had the first string compared to the second string and if the match is not found, then i swap(s1[0], s1[1]), test again, and if match is not found again, then switch back the elements of 0 and 1, and swap elements of 1 and 2, and so on.....
does this algorithm sound ok? kuz.... it's not working on my test! any ideas?
hope this makes sense.

[edited by - red-dragonX on March 18, 2004 12:13:23 AM]

Share this post


Link to post
Share on other sites
first of all, if you are testing only 1 part of your algorithm (that the switching one step at a time, isnt screweing other things up) .. then DONT have the extra stuff at the end of the test ... try these string first:

"thissucks"
"thissucsk"

if THAT does what you want, then you decide what you want it to do if you have a match up to the end of one string .. and make that additional part work ...

also, all of the responses of using a bool, and how to use a bool are correct ... but the implication that your original use of a bool into an int was wrong .. is ... wrong ... in C, EVERY type will automatically and correctly be elevated to any type after it in the following list:

bool, char, short, int, long, float, double

so a bool can be correctly stored / converted to ANY other type. As you noticed, the seg fault was some other problem in your program. But if you WANT to work ona logical boolean, you should use the bool type for clarity ... as they all recommended.

good luck

Share this post


Link to post
Share on other sites