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
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;
You need to use std::boolalpha
to print true/false :
#include <iostream>
int main()
{
std::cout << std::boolalpha << true << std::endl;
}
If you need to do this outside of a stream output <<
, it's a simple ternary expression:
std::string str = (a == b) ? "true" : "false";