In this video, we'll see some of the stream formatting options that are available that
work with any type of data.
These are very powerful and easy to use.
They give you the ability to define a field with
and then display the next data item,
either left justified or right justified within that field.
We can also specify the fill character that will be used to fill up
any unused space.
By default, there is no field with defined.
And all output occurs right after the previous output as we've seen already in this course.
If you do set a field width,
then the next data item displayed will default to right justified
within that field width.
It's important to understand that if you set the field width and justification,
they are only applied to the next data item placed on the output stream.
When we set the fill character, that will persist until you change it.
Again, this all might sound really complicated but it's pretty easy
and a few examples will show you exactly how it works.
But first let's look at the defaults.
Here's an example that uses absolutely no stream manipulators or methods.
This is the default behavior when you put data on a stream.
As you can see, we're displaying the floating point number
1234.5678 and then the string hello.
The number will display with 6 digits of precision and it will round.
And then hello is displayed immediately following the number.
If we modify this program slightly so that it prints the number and the string each on a new line,
then we get the number displayed as before
on the left side with no padding up front followed by the string on the next line.
So far this is what we've seen and it should be pretty clear.
So now let's set the field width of the next data item placed on the stream using setw.
In this case, we're using setw with a 10 as a parameter.
This will create a field width of 10 for the next data item only.
In this case, that data item is num.
Notice that num will be displayed right justified
by default in the field width of 10.
I'm displaying a ruler of numbers just above the actual display
so you can see the alignment.
This ruler isn't displayed by the code.
It's only there so you can better follow the examples.
Notice that the last digit in number, the 7 in this case,
is displayed in position 10.
That's the rightmost side of the field width we set earlier.
But now look what happens when we display hello.
It displays right next to the number starting in column 11.
That's because the field width and the justification only apply to the next
data item placed on the stream. In this case, that data item was num.
So hello has no field width associated with it,
and it uses just the defaults.
In this example, we set the field width of each of the 3 data items to 10.
And remember, when we have a field width, the default justification is right.
So in this case, num is displayed right justified in a width of 10.
The first hello is displayed right justified also in a field of 10.
And the last hello is also displayed right justified in the width of 10.
And you can see from the display in the ruler exactly what's happening.
This example shows how the field width only
applies to the next data item put on the stream.
In this case, we set a field width of 10,
and we want left justification for the next data item, which is num,
and we get what we expect. Num is displayed in a field width of 10,
and it's left justified.
Now we want to display hello.
Notice that hello has no field width associated with it,
so it will display starting in column 11.
In this example, we'll set the field widths for all 3 items displayed.
Num will have a field width of 10, and it will be right justified by default.
The first hello will have a field width of 10,
and we're explicitly setting right justification.
We don't have to do this since it's the default, but we can do it to be very explicit.
And the second hello will have a field width of 15,
and it will also be right justified.
You can see the output is exactly as we expect.
Num is right justified in a field width of 10.
The first hello is right justified in a field width of 10.
And the final hello is right justified in a field width of 15.
Now let's take a look at the setfill formatter.
This only works when a data item has a field width associated with it.
We supply the character that we want to fill any empty space in the given field width.
Here's a simple example.
We're setting the fill character to a dash with the setfill manipulator,
and we're setting the field width to 10.
This will apply only to num, which is the next data item on the stream.
So we expect num to be right justified in a field width of 10,
and the remaining space in that field to be filled in with dashes.
That's exactly what we see. But what about the hello that we display next.
We said that setfill was persistent, but we don't see any dashes with that hello.
That's because that hello doesn't have a field width associated with it.
So setfill only applies when we've set the field width.
So let's look at 1 final example. In this case, we're setting the fill character to an asterisk.
And then setting the field width of num to 10.
We expect num to be displayed right justified in a field width of 10
and asterisk to fill in the remaining space.
And you can see by the output that that's exactly what we get.
Now we set the fill character to a dash,
and the field width to 10 for the first hello.
We expect hello to display right justified in a field width of 10,
and the blank space filled in with dashes, again, that's exactly what we get.
Then finally, we set the field width of the last hello to 15.
We expect hello to display right justified in a field width of 15.
And since setfill is persistent and the last hello does have a field width,
we expect that those blank spaces will be filled in with dashes.
And as you can see, that's what we get.
So you see it's really not that complicated, and the rules are very consistent.
Using the formatting manipulators that we've seen in the last few videos,
we can have a good amount of control over how our output looks.
Now let's head over to the IDE, and we'll see these stream formatters in live code.
Okay. So I'm back. I'm back in the IDE. I'm in the section 19 workspace,
and I'm in the manip_fields project.
And what we'll do here is we'll play around with the field with the justification and the setfill.
You'll notice on line 7 here that I've got a function called ruler
and all that function does is just display a line of numbers,
and that way we can line up our columns easier when we see them.
Okay. You don't have to do this, but it just makes it easier to learn exactly what's going on.
And then I've got these 3 data items here that I'm using.
I'm using num1, which is an integer and it's 1234.
I've got num2, which is a double
1234.5678,
and just a simple string hello.
The first thing we'll do is just like we did in the slides, let's just do the defaults.
So all we're doing is we're putting num1, num2 and hello onto cout.
And when we run that, you'll see everything just runs up together.
So let me build and run. And then we see 1234, that's the integer,
and then 1234.57, rounded 6 significant figures here,
that's our precision, and then the hello. Everything jumbled up together,
right. One right after the other, that's exactly what we expect.
Okay. We can obviously --
let me comment this code out. We can obviously
display one on each line. And sure, that'll help us read it a little nicer
because now we'll see each one each line.
But remember, the defaults are being applied.
So we're getting 1234 as the integer,
1234.57, which is rounding to the 6th
precision for the digit and hello again.
Now let's play around with the field widths.
And here's a simple example. What we're doing is, again, we're displaying the ruler.
And then we're setting the field width to 10 for the next
data item that's being put on the stream, which happens to be num in this case right here.
So num will be displayed in a width of 10.
And if you recall num is that integer 1234.
So that will be displayed in the width of 10, and the default is right justified.
But then when num2 and hello are displayed,
there is no field width associated with them. That field width
only associates with the next item.
So these 2 should print right next to each other and right next to num1.
So let's take a look.
And there you go. You can see 1234 right here.
And let me grab my pencil, and I'll draw in white, I think it'll be easier to see.
You can see right here the 1234, right there, that's our integer,
and that's the 10th, right the 10th column.
So it's being right justified in the 10th column.
But then after that, we're seeing our double
and our string, all printed run right next to the other, no formatting applied.
Now let's do one more example. Actually let's do a few more examples.
Let's do this one here. And the source code's available to you,
I encourage you to go in there and play around and change things around. That's really the best way to learn it.
Okay. So now look what we're doing. We're setting the width
to 10 for num1 and for num2.
So now we expect num1 to be right justified in a width of 10,
num2 to be right justified in the width of 10,
and then hello with no formatting, just put right on the end of num2.
So let's run this.
And here, down at the bottom, you can see that, that's exactly what happens here.
There's my field width of 10. And there's 1234, right justified in there.
Here's the other width of 10 right here the next field.
And you see 1234.57, right justified in there as well.
And there's my hello.
I'm going to put a couple of new lines at the end, just so that
it'll separate that time elapsed message a little bit,
so it'll be a little bit easier to see what's going on. And there they are. I just had them commented out.
So I'll uncomment them.
Okay. So let's take a look at this example now.
I will uncomment this code.
And notice what's happening here, I'm setting the field width to 10
for each one of those items.
By default, we're going to right justify everything.
So num1, num2 and hello will each display right justified in a field width of 10.
And let's give that a run. And there you go. You can see 1234 the integer
in the field width of 10, 1234.57
in its own field width of 10.
And hello, you can see the 0 right there, right. That would be the 30th column,
the third field width of 10, and it's right justified.
Comment this out and see what this is doing.
Now this is doing exactly the same thing except
we're left justifying each one of these now.
Remember, we have we want to left justify then we have to say it explicitly,
right justified is is the default.
I always like to put in left and right that way whoever's reading my code afterward
understands exactly what's going on.
Even though, we know it's the default
somebody might have forgotten, so it's nice to see that there.
So here width of 10, left justified num1,
width of 10 left justified num2, and width of 10, left justified hello.
So let's give that a run.
And you'll see the difference between
this guy right here where they were right justified and now down here where they're left justified.
So you can see that 1234 is displaying in column 1,
1234.57 in column 11,
and hello in column 21,
just like we expect. Okay. So now let's do a couple with the setfill,
and I'll comment this out, we'll walk through this in a minute. So notice what I'm doing here, I'm setting
the fill character to the dash.
Now this is a persistent setting. This will persist until I change it.
And what I'm doing here is I'm setting the width to 10
left justified num1.
Now num1 was only 4 digits, right.
So the rest of the space in that field of 10 is going to be
padded or filled with that dash character.
And the same thing will happen to these guys.
Remember, if we don't have a width set, then setfill doesn't apply.
It only applies when we're setting the field width. So in this case, we expect to see
all 3 of these guys. Left justified in a field width of 10.
And all the spaces that are left over,
all the blank spaces in that field of 10 filled with dashes. So let's take a look.
And there you can see right down here.
You can see 1234, and then dashes all the way to the 10th position.
And then 1234.57 dashes all the way to the 20th position.
And then hello and dashes all the way to the 30th position.
Okay. Had we right justified them the dashes would have been on the left.
It would have filled any empty space in that field width.
And the last example is
we're doing now is we're using different setfill characters.
So we're setting the filled width to 10 for each one of them again,
and we're left justifying. And in this first example for num,
I'm using the asterisk as the fill character. For num2,
I'm using the hash mark or the pound symbol.
And for hello, I'm using the dash.
We should see 3 different fill characters filling up all that space.
Remember, once this statement is done,
this is the fill character that's set, so that's going to persist
until I change it otherwise. All right. So let's run this.
And there you can see that I'm using the asterisks for the first field,
the pound signs or the hash symbols for the second field
and the dashes for the final field.
Okay. So that's it for this video. In the next video,
we've got a challenge where we're going to put some of this stuff together to display a table.
Now you get a chance to play with them and put them together and understand how you can use
your precision, your left, your right, your fixed and so forth all together
to create a table of tour prices for trips to South America.
So that's the challenge coming up in the next video.