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.
C++ manipulator boolalpha function is used to set the output stream to display bool values as the strings "true" or "false"
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.
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";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With