Boolean variables in C++ are binary data that can be true or false, whereas string variables represent a sequence of the alphabet, numerals, as well as special characters. The compiler does not support translation from string to Boolean, but there are multiple techniques for doing so.
In this article, we will look at various methods for converting the string value to a Boolean value in C++.
How to Convert the String to the Boolean Value in C++?
We consider the technique, which is quite simple. We accept a value that is a string and convert this to a Boolean value in several methods. A generalized algorithm is given below:
-
- Take data into a string variable.
- Convert a string value to a Boolean.
- Output the value.
Now we are going to explain the possible methods to convert a string value into a Boolean value in C++.
1: Using stoi() Function
In some cases, the string value could be 0 or 1. In that situation, we can use the stoi() function to convert the string number to an integer and then to a Boolean. The stoi() function changes string values to integers, which can then be converted to Booleans using explicit typecasting. Consider the example that illustrates the use of stoi() function to convert string value to Boolean value.
#include <string>
using namespace std;
bool my_function(string str)
{
return (bool)stoi(str);
}
int main()
{
string input1 = "1";
bool output1 = my_function(input1);
cout<< "The input string value is: " << input1 << endl;
cout<< "The output bool value is: " << output1 << endl;
string input2 = "0";
bool output2 = my_function(input2);
cout<< "The input string value is: " << input2 << endl;
cout<< "The output bool value is: " << output2 << endl;
return 0;
}
In the provided example code, the my_function function takes a string as input and returns a Boolean value by converting the string to an integer using stoi() function and then casting it to a Boolean using the (bool) typecast. The main function calls my_function with the string “11”, which is converted to the integer value 11 and then cast to the Boolean value true. Finally, the input and output values are printed to the console using cout.
Output
2: Using String Comparison
We will use this technique to conduct the basic string comparison for converting a string value to a Boolean value. If the string’s value is ‘false,’ the value 0 is returned; alternatively, the value 1 is returned. For example:
#include <string>
using namespace std;
bool my_function(const string& str) {
return str == "true" || str == "1";
}
int main() {
string input1 = "true";
bool output1 = my_function(input1);
cout<< "The input string value is: " << input1 << endl;
cout << "The output bool value is: " << output1 << endl;
string input2 = "false";
bool output2 = my_function(input2);
cout<< "The input string value is: " << input2 << endl;
cout << "The output bool value is: " << output2 << endl;
return 0;
}
In this example, we define a function called my_function that takes a const reference to a string argument and returns a boolean value based on whether the string contains the text “true” or the digit “1”. The function simply uses string comparison to check if the input string matches one of these values, and returns true or false accordingly.
Output
3: Using Equality Operator
We can use the string object’s (==) operator to determine whether the value of the string is “0” or not. If it is “0,” it is converted to the Boolean value false; otherwise, it is converted to the Boolean value true. Let’s look at an example:
#include <sstream>
#include <string>
using namespace std;
int main()
{
string input1 = "0";
bool output1 = !(input1 == "0");
cout<< "The input value is: " << input1 << endl;
cout<< boolalpha<<"The output value is: " << output1 << endl;
string input2 = "1";
bool output2 = !(input2 == "0");
cout<< "The input value is: " << input2 << endl;
cout<< boolalpha<<"The output value is: " << output2 << endl;
return 0;
}
The above code demonstrates the conversion of a string to a Boolean value. It first initializes a string variable ‘input’ with the value “0”. It then converts this string to a Boolean value using the expression !(input == “0”). Finally, it outputs the input and output values using the cout statement. The boolalpha manipulator is used to output the Boolean value as “true” or “false” instead of 1 or 0.
Output
4: Using istringstream() Function
Using this method, we will construct an istringstream object and initialize it using the given string value in this method. Then, from this istringstream object, retrieve the Boolean value. If the string contained a numeric value but not 0, the Boolean value will be true. Otherwise, the retrieved bool result is false. Let’s look an example:
#include <string>
#include <sstream>
using namespace std;
int main()
{
string input1 = "0";
bool output1;
istringstream(input1) >> output1;
cout<< "The input value is: " << input1 << endl;
cout<< boolalpha<<"The output value is: " << output1 << endl;
string input2 = "1";
bool output2;
istringstream(input2) >> output2;
cout<< "The input value is: " << input2 << endl;
cout<< boolalpha<<"The output value is: " << output2 << endl;
return 0;
}
The above code reads a string input “1” and converts it to a Boolean value using istringstream(). The bool output variable is initialized but not assigned a value until istringstream() is used to read the input value. The Boolean value is then output using cout.
Output
Conclusion
When third-party libraries or APIs are utilized in a project, it’s required to convert a string value into a Boolean value. Some APIs or libraries produce in string format, and we must convert the string values to Boolean to make the results compatible. To perform it we have discussed various methods along with examples in this article to convert a string to a Boolean value in C++.