-4

I want to include a function in my class to set the value of a boolean to the desired state. However, I am not sure about the syntax, especially because I do not know what type "true/false" is.

I was thinking about something similar to:

void setBool(boolean bBool, string str){
    bBool = str;
}

int main (){
    bool myBool;
    string str = "false";
    setBool (myBool, str);
    return 0;
}

Does anybody have any ideas? What datatype would "true/false" be?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
alto125
  • 19
  • 6

1 Answers1

1

bool's are a type of integer datatype that represents true or false. They do this by the number they hold. A zero value is false and all other values are true. true and false themselves are actually keywords. If you want to convert the string into a bool then you can do it like

void setBool(bool& bBool, const string& str)
{
    if (str == "false")
        bBool = false;
    else
        bBool = true;
}

You could also store the result of a comparison in a bool as a comparison returns a bool. So if we do:

bBool = (str != "false");

Then if str is not equal to "false" then that will be true and bBool will be true otherwise it will be set to false. So no the function would be:

void setBool(bool& bBool, const string& str)
{
    bBool = (str != "false");
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402