How to convert boolean to string in C++

I am brand new to C++. I am trying to write a program to test the truth table and struggling with boolean type. I have the user input as T/F and program output as "True" or "False". I tried to use char for input/output, it works but it only allow "T" or "F". I also tried to use boolean, but the output is 0 or 1. How can I convert boolean to string in the output? Is there anyway to validate user input (not null) before the program run?

Thanks.
Well you could do something using if's and else's. Say something like

1
2
3
4
5
6
7
8
char input;
cin >> input;
if(input == 't' || input == 'T')
      cout << "True" << endl;
if else(input == 'f' || input == 'F')
      cout << "False"  << endl;
else
      cout << "Your input was invalid" << endl;

Last edited on
Use the boolalpha stream manipulator.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <limits>
using namespace std;

int main()
  {
  bool b;

  cout << "Enter a boolean value> ";
  while (true)
    {
    cin >> boolalpha >> b;  // read a boolean value
    if (!cin)
      {
      cin.clear();
      cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
      cout << "That was not a boolean value! Try again> ";
      }
    else break;
    }
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

  cout << "You entered \'" << boolalpha << b << "\'.\n";
  cout << "The negation is \'" << boolalpha << (!b) << "\'.\n";

  cout << "\nGood-bye!\n";

  return 0;
  }

The caveat is that the boolalpha flag is too dumb to recognize anything but "true" and "false" (not even variations in letter-case).

If you want something better, let me know. :-)
Thanks.
I use boolalpha in reading inputs and print output so I can save time on writing if...else statements.

I am wondering if there is function like CBool in C++?
Alas, there isn't. IMO the boolalpha should do it, but it doesn't. You'd have to write it yourself.

Give me a day or too and I'll write you a little manipulator that does it. :-)
Last edited on
Sorry it has taken me so long to respond again. I'm just now messing with a custom boolalpha manipulator, but I have a question for you:

In C and C++, numbers may be represented in any of three bases:
hex 0x1B oct 033 dec 27

However, the standard istreams are apparently too stupid to figure out the difference without some help from the programmer using the appropriate ios_base::hex, oct, and dec baseflags. And combining them (using istream::flags()) doesn't work either.

But, the whole point of the custom manipulator is to allow some automatic variations in recognized input format: accepting "true" or "True" or "yes" or "1" etc for true, and likewise "false" or "F" or "n" or "0" or whatever for false.

In the case that the input is numeric, should I consider the user's baseflags? If so, should I allow combinations?

Or should I just ignore the baseflags (seeing as we are reading a bool value, whether expressed as a number or as text) and accept any valid C++ formatted radix?

Your thoughs appreciated. [edit] Right now I'm leaning towards the second option...[/edit]
Last edited on
Topic archived. No new replies allowed.