C++ Program to convert Boolean Variables into String


Boolean variables in C++ can contain only two distinct values, ‘true’ or ‘false’. If we translate these values to string, ‘true’ will be mapped to ‘1’ and ‘false’ will be mapped to ‘0’. Boolean values are mainly used to check if a condition in a program has been met or not. There aren’t direct conversions from Boolean to string like we have seen in conversions from int to long and float to double. But there are needs to translate a Boolean value to string, and we explore different ways to convert a binary Boolean value to a string value.

Using ternary operator to translate

We have devised an algorithm, using which we check the value of the provided Boolean variable, and then output ‘true’ or ‘false’ based on the value. The output is a string variable, whereas the input is a Boolean value. We have used the ternary operator to determine the output as there are only two Boolean values.

Syntax

bool input = <Boolean value>;
string output = input ? "true" : "false";

Algorithm

  • Take a boolean value as the input;
  • If the boolean value is true, then the output will be the string ‘true’.
  • Else if the boolean input value is false, then the output value will be ‘false’.

Example

#include <iostream> using namespace std; string solve(bool input) { //using ternary operators return input ? "true" : "false"; } int main() { bool ip = true; string op = solve(ip); cout<< "The input value is: " << ip << endl; cout<< "The output value is: " << op << endl; return 0; }

Output

The input value is: 1
The output value is: true

The input value is stored in a variable ip, and the conversion operation is done in the function solve(). The output of the function is stored in a string variable which is op. We can see the output of both the variables. The first value in the output is before conversion and the second value in the output is after the conversion.

Using std::boolalpha while string output

boolalpha is an I/O manipulator, so it can be used whilst in a stream. The first method which we will discuss cannot be used this method to assign a boolean value in a string variable, but we can use it to output it in the format in an input/output stream.

Syntax

bool input = <Boolean value>;
cout<< "The output value is: " << boolalpha << input << endl;

Algorithm

  • Take a boolean value as the input.
  • Display the Boolean value as output using the boolapha modifier.

Example

#include <iostream> using namespace std; int main() { bool ip = true; cout<< "The input value is: " << ip << endl; cout<< "The output value is: " << boolalpha << ip << endl; return 0; }

Output

The input value is: 1
The output value is: true

In the above example we can see that if we output the value of the boolean variable using cout, the output is 0 or 1. When we are using the boolalpha in the cout, we can see the output changes to a string format.

Using std::boolalpha and assigning it to a variable

In the previous example we have only modified the output stream to get the string output of a boolean value. Now we see how we can use this to store the string value in a variable.

Syntax

bool input = <Boolean value>;
ostringstream oss;
oss << boolalpha << ip;
string output = oss.str();

Algorithm

  • Take a boolean value as the input.
  • Put the input value in an outputstream object using the boolalpha modifier.
  • Return the string format of the outputstream object.

Example

#include <iostream> #include <sstream> using namespace std; string solve(bool ip) { //using outputstream and modifying the value in the stream ostringstream oss; oss << boolalpha << ip; return oss.str(); } int main() { bool ip = false; string op = solve(ip); cout<< "The input value is: " << ip << endl; cout<< "The output value is: " << op << endl; return 0; }

Output

The input value is: 0
The output value is: false

Unlike the previous example, we are taking the input boolean value in an outputstream and then converting the value to string. The solve() funvtion returns a string value and we are storing the value in the op variable in the string function.

Conclusion

We discussed the various ways to convert a binary boolean value to a string. These methods are useful while we are handling databases or interacting with some web-based APIs. The API or the database methods may not accept boolean values, so using these methods we can convert it to a string value and thus any methods accepting string values can also be utilised.

Updated on: 19-Oct-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements