boolean how to print "on" "off" instead of 1 and 0

I am doing multiple inheritance

this is my display function

void HotAirBalloon::display()
{
Balloon::display();
Basket::display();
cout << "Burner: " << burner << endl;

}

this is my int main

int main()
{

bool burner;
cout << "Enter burner(on=1/off=0): " << endl;
cin >> burner;

HotAirBalloon hab(burner);

hab.display();

return 0;
}

I saw online that using cout.setf(std::ios_base::boolalpha); but is there any other way?

I want to print "on" or "off" instead of 1 and 0.What can i do?
Thanks in advance
Last edited on
Sorry I just figured it out
puting
if(burner)
{
cout << "On" << endl;

}
else
{
cout << "Off" << endl;
}
cout << "--------------------" << endl;

in display(); will do
string onoff[] = {"Off", "On};
...
cout << onoff[burner];
An alternative to the if/else block, the conditional operator:

std::cout << (burner ? "On\n" : "Off\n");

https://www.learncpp.com/cpp-tutorial/comma-and-conditional-operators/

Last edited on
Topic archived. No new replies allowed.