I have overloaded equals (including == and !=) that checks if two objects are equals and then returns a boolean.
Unfortunately, it prints 0 or 1. I know it's correct but I can't figure out the way to make it to print true or false for readability purposes.
I've even tried:
if (a.equals(b)) { return true; } return false;
However, C++ is stubborn enough to output 0 or 1.
Any help would be appreciated.
Edit - Print is done:
cout << "a == b is " << (a == b) << endl;
Desired output is
a == b is true
Boolean Variables and Data Type ( or lack thereof in C ) C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.
bool "bar" is by default true, but it should be false, it can not be initiliazied in the constructor. is there a way to init it as false without making it static?
In C, most things we think of as boolean are actually int (0 or 1). We prefer to use bool return type for functions which have 2 return values ( true or false ).
You can use std::boolalpha
:
Sets the boolalpha format flag for the str stream.
When the boolalpha format flag is set, bool values are inserted/extracted as their names: true and false instead of integral values.
This flag can be unset with the noboolalpha manipulator.
The boolalpha flag is not set in standard streams on initialization.
std::cout.setf(std::ios::boolalpha); std::cout << true;
or
std::cout << std::boolalpha << true;
You need to use std::boolalpha
:
cout << boolalpha << yourthing() << endl;
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