C++ bool returns 0 1 instead of true 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;

Leave a Comment