need help with this bool function..

Started by
6 comments, last by red-dragonX 20 years, 1 month ago
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.
Advertisement
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
[/quote]
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 belowbool 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
Half the people you know are below average.Trogdor the Burninator
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]
[/quote]
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?
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]
[/quote]
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]
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

This topic is closed to new replies.

Advertisement