In this video, we'll see how to format Boolean types to output streams.
Note that we're using cout in these examples,
but any output stream will work, including file output streams,
which we'll learn about later in this section.
By default, a Boolean value is displayed as a 1 if the value is true
or is a 0 if the value is false.
But sometimes, we'd like to display the strings true or false.
And we really don't want to write if else logic all over our code every time we need to do that,
instead, we can tell our output stream to display Boolean values as
01 or as true false,
whichever we want.
First, let's see an example of the default behavior for Boolean values.
First, we display the value of the expression 10 equals 10.
Since this is true, a 1 will be displayed.
Then we display the value of the expression 10 equals 20,
which is false, and as you see a 0 is displayed.
Now let's use a stream manipulator to switch this to true false mode.
In the first statement,
we're setting the output stream c out to bool alpha mode.
This means that all further output to cout
will display true or false as strings.
And you can see the output from the same program now displays true and false.
It's important to understand that with Boolean formatting
once we set it with bool alpha or noble alpha,
then all further Boolean values will be displayed that way.
We can toggle back and forth as appropriate as shown in this slide.
We can also set the format for Boolean types using the setf method.
In this case, we have a bunch of flags that can be used to set
and reset formatting directives.
In this example, we can pass std IOS bull alpha
or std IOS no bool alpha to the cout object
to change its formatting.
Finally, we also have a method named reset ios flags
that expects the flag to reset.
In this case, we can pass in std ios bool alpha,
and it will reset the stream to whatever the default is for that flag.
In this case, it will go back to displaying Booleans as 0s and 1s.
Although you sometimes see this type of code in production,
it's more common to see the manipulator version,
and that's what I'll focus on in this section.
So let's head over to the IDE. We'll do a couple of examples using bool alpha and no bool alpha.
Okay. So I mean the IDE. I mean the section 19 workspace
in the manip_Boolean project.
And in this project, I've got a bunch of simple test cases here that just display
certain values to standard out or cout.
or cout.
In this case, those values are Boolean values, and we're going to manipulate
them using the no bool alpha and bool alpha
manipulators.
So here's the default.
What we're doing here, we're going to use this same test case throughout.
You can see what we've got here, we've got --
word is 10 equal to 10, that's either true or false. Obviously, this is true, so this will
always display true. And 10 equals 20 will never be true so that'll always display false.
In this case, we're using an expression, but we could certainly use a variable
anything that evaluates to a true false value.
So by default, this will display true, which is a 1,
and this will display false, which is a 0.
Okay. So by default, that is the way that we display Boolean values as we saw in the slides.
So why don't we run this program, and then we'll start
modifying it just a little bit so we can start displaying the actual string true faults.
Okay. So let me run this.
And you can see right here, I'll move this over here so you can see what's going,
on bool alpha is the default.
I didn't set it, but it was just the default you can see I've set nothing here.
All I'm doing is those 2 cout statements.
And I'm getting a 1 here, and I'm getting a 0 here, exactly what I expect.
Okay. So now let's make a few changes here.
So you can see what I'm doing here on line 14.
I'm inserting std bool alpha onto cout. It's a manipulator.
This will switch the default formatting in this case now
to true false.
Now it's important to understand, and we'll show you on lines 19 and 20 that this is true.
Once we set that everything else whenever you need to display a Boolean value,
this is the setting that will stick.
Okay. So if we need to go back to 0 1, we have to do that manually.
All right. So what we expect here now is to actually display the word true
and the word false. So let's give this a run.
And here you go. You can see the original 2 statements 1 0, but now we're seeing true false.
Okay. So here all we're doing is showing that
once we set it it's set, right. So here it's set to true false,
and we just displayed 2 statements to cout.
Now we've got another 2, and we expect those to be true false as well.
There you go, true false, true false. It's going to keep doing this until you change it.
And that's what we're going to do here on this lines 23 to 25.
I'm using std no bool alpha, so this is toggling it back
to the default of 0 1.
And at this point, we should be getting 0 1.
So I should get, again, 1 0
true false, true false and then 1 0 again.
And that's exactly what I'm getting, 1 0 true false, true false and then 1 0 here.
Okay. So that's pretty straightforward. And believe it or not,
what you're seeing here holds for every 1 of the manipulators
that we're going to see. So it's the same kind of behavior,
not the part of once you set it, it's set some of them just work for
1 insertion, but we'll talk about those.
But I'm talking about the syntax and the general flow here.
It's pretty much the same for all of them.
So let me comment out these lines, 28 to 30.
And in this case, I'm doing the same thing except I'm not using the manipulator,
I'm using the setf method. So you can see I'm saying std cout.setf.
Setf is the method, and it expects
a std ios bool alpha.
Now that looks maybe kind of intimidating and nasty, it's not so bad. Std is the namespace,
ios is a class and bool alpha is a flag that's defined in that class.
Okay. So c++ is doing exactly what it was expected to do.
If you just type bool alpha, it won't know where to find it.
So you have to say it's std namespace ios class bool alpha flag.
If you're using namespace standard, then you don't need to put std here
or here or anywhere here that you see it lighting up right, you could just use ios bool alpha.
Okay. So in this case, again, I'm setting it
back to true false, but I'm doing it with this method.
Now so you should see those last 2 statements display the words true false,
and there they are right here. You can see there's true and here's false.
And then the last one is used to reset
certain flags.
All of these flags that we're going to use have default values.
Okay. So when we talk about
left and right justification and
you know the way we display numbers
or the way we display Booleans is in this case, they have default ways of doing it.
We know the default for the Booleans is the numbers 0 or 1,
right. So there is a
manipulator here called reset ios flags.
And again, it's in std namespace, so reset ios flags,
and then you pass into it whatever flag you want to reset.
And it will reset that
not to bool alpha, but it'll reset it to the default behavior for
displaying Booleans, which, in this case, will be numbers, right, 0 1.
So when we run this, we expect that to be reset to the default.
And what's nice about this is, suppose you've got a modifier that has --
there aren't any like this, but suppose you had 8 different values, right,
1 of them is obviously see the default, you don't have to worry about which one to set. You can just
basically call this with the
flag you want, and it'll reset it to the appropriate behavior.
So let's run this, and now we expect that those last 2 statements to print out 1 and 0.
And there you go.
So that's it. The the basics of everything else
we're going to do with the manipulators are here.
We can use them in line. We can use them with functions.
And what we'll do next is we'll talk about integer output.
output. So we'll be right back to do that.