Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boolalpha capitalize

Tags:

c++

How do you capitalize the output generated by the cout of boolean values.

I know that if I did:

cout << boolalpha << true;

it will output

true

how do I get it to output

True

I have some feeling it has to do with do_truename and do_falsename, but I have no clue how to do it.

like image 257
Gideon Avatar asked Nov 28 '12 01:11

Gideon


People also ask

What does Boolalpha mean?

C++ manipulator boolalpha function is used to set the output stream to display bool values as the strings "true" or "false"

How does Boolalpha work in C++?

The boolalpha() method of stream manipulators in C++ is used to set the boolalpha format flag for the specified str stream. Parameters: This method accepts str as a a parameter which is the stream for which the format flag is affected. Return Value: This method returns the stream str with boolalpha format flag set.


1 Answers

For a fleeting moment I thought that this could be done using std::uppercase but this doesn't seem to be the case: these only apply to things like the hexadecimal digits and the exponent. So, it seems it, indeed, requires a std::numpunct<char> override which is, however, not that bad:

#include <iostream>
#include <locale>

struct numpunct
    : std::numpunct<char>
{
    std::string do_truename() const { return "True"; }
    std::string do_falsename() const { return "False"; }
};

int main()
{
    std::locale loc(std::cout.getloc(), new numpunct);
    std::cout.imbue(loc);
    std::cout << std::boolalpha << true << "\n";
}
like image 145
Dietmar Kühl Avatar answered Sep 20 '22 23:09

Dietmar Kühl