boolalpha capitalize - 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.

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";
}

Related

Convert boost::multiprecision::cpp_dec_float_100 to string with precision

In my code I want to have a function which performs some calculations based on boost::multiprecision::cpp_dec_float_100 and returns string as a result with some precision, but the question is how I should set the precision ? e.g. If the precision is 9, then I expect the following results for the following numbers:
for 2345.12345678910111213 I expect string "2345.123456789" but I have 12345.1235
for 1.12345678910111213 I expect string "1.123456789" but I have 1.12345679
So it always returns my exact number of characters provided to str() function with round. I know that I can do it like e.g. this:
std::stringstream ss;
ss << std::fixed << std::setprecision(9) << my_value;
return ss.str();
or use cout(but this is not my case) or do some find on string to find "." and take only 9 characters after it, but can I do it in an easier way ? maybe in boost is some function to do it correctly ?
#include <iostream>
#include <boost/multiprecision/cpp_dec_float.hpp>
namespace bmp = boost::multiprecision;
std::string foo_1()
{
bmp::cpp_dec_float_100 val{"12345.12345678910111213"};
return val.str(9);
}
std::string foo_2()
{
bmp::cpp_dec_float_100 val{"1.12345678910111213"};
return val.str(9);
}
int main()
{
auto const val_1 = foo_1();
auto const val_2 = foo_2();
std::cout << "val_1: " << val_1 << std::endl;
std::cout << "val_2: " << val_2 << std::endl;
return 0;
}
online version: https://wandbox.org/permlink/HTAHsE5ZE3tgK9kf
Change the line:
return val.str(9);
To:
return val.str(9, std::ios::fixed);
You will get the expected strings.

Can I set a variable that will change the floatfield when using cout?

I want to be able to easily change the floatfield from scientific to fixed dependent on a condition, so my result would change from something like 3.000000e+00 to 3.0, using code that might look something like this:
some_datatype floatfield;
float number = 5.894
if (condition) {
floatfield = 'scientific';
} else floatfield = 'fixed';
cout << "hello" << floatfield << number << endl;
I'm aware I can achieve the same effect using
float number = 5.894
if (condition) {
cout << "hello" << scientific << number << endl;
} else cout << "hello" << fixed << number << endl;
But this does not generalise well if I start using more stream format flags.
If some_datatype exists, what is it? If not, is there any other way of changing the stream format flags using a condition?
I/O manipulators are actually functions (!), and the ones you want to switch between happen to have the same signature.
So, you can use a function pointer to achieve your goal:
#include <iomanip>
#include <iostream>
int main()
{
using ManipFuncType = std::ios_base&(std::ios_base&);
const bool condition = true; // or false!
ManipFuncType* floatfield;
float number = 5.894;
if (condition)
floatfield = std::scientific;
else
floatfield = std::fixed;
std::cout << "hello" << floatfield << number << std::endl;
}
(live demo)
I'm not sure I understood the question correctly, but this could be a solution:
#include <iostream>
using namespace std;
struct my_float {
float value_;
bool scientific_ = true;
my_float(float value) : value_(value) {}
void set_scientific() { scientific_ = true; }
void set_fixed() { scientific_ = false; }
};
ostream& operator<<(ostream& os, const my_float& f) {
if (f.scientific_)
return os << scientific << f.value_;
else
return os << fixed << f.value_;
}
int main() {
my_float number = 5.894;
bool condition = true; // <--- Change this!
if (condition) {
number.set_scientific();
}
else {
number.set_fixed();
}
cout << "hello" << number << endl;
return 0;
}
This is of course very crude, and the class could definitely be more complete.
Don't use this to store many floats, because id may easily double your memory requirements.

Why is boolean printed as a number? [duplicate]

I print a bool to an output stream like this:
#include <iostream>
int main()
{
std::cout << false << std::endl;
}
Does the standard require a specific result on the stream (e.g. 0 for false)?
The standard streams have a boolalpha flag that determines what gets displayed -- when it's false, they'll display as 0 and 1. When it's true, they'll display as false and true.
There's also an std::boolalpha manipulator to set the flag, so this:
#include <iostream>
#include <iomanip>
int main() {
std::cout<<false<<"\n";
std::cout << std::boolalpha;
std::cout<<false<<"\n";
return 0;
}
...produces output like:
0
false
For what it's worth, the actual word produced when boolalpha is set to true is localized--that is, <locale> has a num_put category that handles numeric conversions, so if you imbue a stream with the right locale, it can/will print out true and false as they're represented in that locale. For example,
#include <iostream>
#include <iomanip>
#include <locale>
int main() {
std::cout.imbue(std::locale("fr"));
std::cout << false << "\n";
std::cout << std::boolalpha;
std::cout << false << "\n";
return 0;
}
...and at least in theory (assuming your compiler/standard library accept "fr" as an identifier for "French") it might print out faux instead of false. I should add, however, that real support for this is uneven at best--even the Dinkumware/Microsoft library (usually quite good in this respect) prints false for every language I've checked.
The names that get used are defined in a numpunct facet though, so if you really want them to print out correctly for particular language, you can create a numpunct facet to do that. For example, one that (I believe) is at least reasonably accurate for French would look like this:
#include <array>
#include <string>
#include <locale>
#include <ios>
#include <iostream>
class my_fr : public std::numpunct< char > {
protected:
char do_decimal_point() const { return ','; }
char do_thousands_sep() const { return '.'; }
std::string do_grouping() const { return "\3"; }
std::string do_truename() const { return "vrai"; }
std::string do_falsename() const { return "faux"; }
};
int main() {
std::cout.imbue(std::locale(std::locale(), new my_fr));
std::cout << false << "\n";
std::cout << std::boolalpha;
std::cout << false << "\n";
return 0;
}
And the result is (as you'd probably expect):
0
faux
0 will get printed.
As in C++ true refers to 1 and false refers to 0.
In case, you want to print false instead of 0,then you have to sets the boolalpha format flag for the str stream.
When the boolalpha format flag is set, bool values are inserted/extracted by their textual representation: either true or false, instead of integral values.
#include <iostream>
int main()
{
std::cout << std::boolalpha << false << std::endl;
}
output:
false
IDEONE

C++: converting wstring to double

Before converting wstring to double - how to validate it with regex? Java no problem, but C++ raising questions.. :)
I suppose you have a string and you want to know if it is a double or not. The following code does not use regular expressions. Instead it initializes a stringstream and reads a double from it. If the string starts with something non-numeric, then ss.fail() will be set. If it starts with a number, but does not read the whole string, then there's something non-numeric at the end of the string. So if everything went well and the string is really only a number, then ss.eof() && !ss.fail() will be true.
#include <iostream>
#include <sstream>
int main()
{
std::stringstream ss("123.456");
double mydouble;
ss >> mydouble;
if (ss.eof() && !ss.fail())
std::cout << "yay, success: " << mydouble << std::endl;
else
std::cout << "that was not a double." << std::endl;
return 0;
}
There's also std::wstringstream if you need to convert wide character strings.
You might also want to have a look at the boost libraries, especially at Boost.Lexical_Cast.
With this library you could do the following:
#include <boost/lexical_cast.hpp>
#include <iostream>
int main()
{
try
{
double mydouble = boost::lexical_cast<double>("123.456");
std::cout << "yay, success: " << mydouble << std::endl;
}
catch(const boost::bad_lexical_cast &)
{
std::cout << "that was not a double." << std::endl;
}
return 0;
}
Or maybe it is simpler to do that this way:
std::wstring strKeyValue = "147.sd44";
double value = (double) _wtof(strKeyValue.c_str());
And if strKeyValue==0 then it means it's not double.

C++ printing boolean, what is displayed?

I print a bool to an output stream like this:
#include <iostream>
int main()
{
std::cout << false << std::endl;
}
Does the standard require a specific result on the stream (e.g. 0 for false)?
The standard streams have a boolalpha flag that determines what gets displayed -- when it's false, they'll display as 0 and 1. When it's true, they'll display as false and true.
There's also an std::boolalpha manipulator to set the flag, so this:
#include <iostream>
#include <iomanip>
int main() {
std::cout<<false<<"\n";
std::cout << std::boolalpha;
std::cout<<false<<"\n";
return 0;
}
...produces output like:
0
false
For what it's worth, the actual word produced when boolalpha is set to true is localized--that is, <locale> has a num_put category that handles numeric conversions, so if you imbue a stream with the right locale, it can/will print out true and false as they're represented in that locale. For example,
#include <iostream>
#include <iomanip>
#include <locale>
int main() {
std::cout.imbue(std::locale("fr"));
std::cout << false << "\n";
std::cout << std::boolalpha;
std::cout << false << "\n";
return 0;
}
...and at least in theory (assuming your compiler/standard library accept "fr" as an identifier for "French") it might print out faux instead of false. I should add, however, that real support for this is uneven at best--even the Dinkumware/Microsoft library (usually quite good in this respect) prints false for every language I've checked.
The names that get used are defined in a numpunct facet though, so if you really want them to print out correctly for particular language, you can create a numpunct facet to do that. For example, one that (I believe) is at least reasonably accurate for French would look like this:
#include <array>
#include <string>
#include <locale>
#include <ios>
#include <iostream>
class my_fr : public std::numpunct< char > {
protected:
char do_decimal_point() const { return ','; }
char do_thousands_sep() const { return '.'; }
std::string do_grouping() const { return "\3"; }
std::string do_truename() const { return "vrai"; }
std::string do_falsename() const { return "faux"; }
};
int main() {
std::cout.imbue(std::locale(std::locale(), new my_fr));
std::cout << false << "\n";
std::cout << std::boolalpha;
std::cout << false << "\n";
return 0;
}
And the result is (as you'd probably expect):
0
faux
0 will get printed.
As in C++ true refers to 1 and false refers to 0.
In case, you want to print false instead of 0,then you have to sets the boolalpha format flag for the str stream.
When the boolalpha format flag is set, bool values are inserted/extracted by their textual representation: either true or false, instead of integral values.
#include <iostream>
int main()
{
std::cout << std::boolalpha << false << std::endl;
}
output:
false
IDEONE