Using stringstream to input/output a bool value
You should always verify if your input was successful: you'll find it was not. You want to try the value 1
with your current setup:
if (lineStream >> active) {
std::cout << active << '\n';
}
else {
std::cout << "failed to read a Boolean value.\n";
}
If you want to be able to enter true
and false
, you'll need to use std::boolalpha
:
if (lineStream >> std::boolalpha >> active) {
std::cout << std::boolalpha << active << '\n';
}
The formatting flag changes the way bool
is formatted to use locale-dependent strings.
Try using the boolalpha
manipulator.
lineStream >> boolalpha >> active;
cout << boolalpha << active << endl;
By default, streams input and output bool
values as tiny numbers. boolalpha
tells the stream to instead use the strings "true" and "false" to represent them.