-
>> Hey, guys, Welcome back.
-
Professor Hank here.
-
So today, we're
going to be
-
talking about how you can
-
format your console
output in C++.
-
Specifically, we're
going to talk about
-
these things called
IO manipulators,
-
and the IO manipulators
-
that we're going to look
-
at are going to
be set precision.
-
We're going to
look at fixed,
-
we're going to look
at Show Point, left,
-
right, and set W.
-
So let's go
ahead and begin.
-
So let's learn how
-
this works
through examples.
-
Now, we'll see how set
precision works first.
-
Now, let's say that
I wanted to create
-
a variable that stores
the contents of Pi.
-
And we'll store it out to
-
a certain level
of precision.
-
We'll go out to this
many decimal places.
-
And if I wanted to
-
display that
on the screen,
-
it would look
something like this.
-
You can see we only see
-
3.14159. Now, why is that?
-
That's Because
C++ by default
-
will only show you so
many digits per field.
-
So this variable
contents here,
-
this makes up a field.
-
And so the limit is
six total digits.
-
Now, if we
wanted to change
-
how many digits we
-
are going to see,
can we do that?
-
Well, the answer
is yes and that's
-
where set precision
is going to come in.
-
So to access
such precision,
-
we have to do pound
include IO manip,
-
and then that gives us
-
the ability to do
something like this.
-
So we can send
-
set precision to
see out and say,
-
well, no, we
only want to see
-
one digit there.
-
And the set precision
has to come
-
before the field that
you're modifying.
-
So let's see how
that changes things.
-
So you can see
now we only see
-
the one digit,
the one place.
-
Now, what if I
wanted to see two?
-
Well, then we'll
just change
-
what we put inside
the parentheses.
-
And so now you'll see
that we see the 3.1.
-
So you can notice
two things.
-
One, we just increase
-
the number inside of here.
-
Two, we're moving
from left to
-
right from the
furthest left digit.
-
So let's say we wanted
to see three digits.
-
You just keep
on doing this.
-
You can see now that
it grows to 3.14.
-
And you can just
keep right on going.
-
So we could do show
us four digits.
-
Show us five digits.
-
And so this behavior
of showing us ever
-
increasing number of
digits from left to
-
right from the
furthest left digit,
-
this is an example
of displaying
-
the contents of
the variable or
-
the number from the
most significant digit.
-
What's the most
significant digit here?
-
It's three.
-
And so right now we
have a precision
-
of the last number
-
displayed from the
most significant digit
-
of five digits.
-
Now, what if I wanted to
-
limit to a certain number
-
of decimal places what
I wanted to show.
-
Let's say I wanted to show
-
two decimal places
of precision.
-
Well, one way
you can do that
-
is to use set
precision like this.
-
You just say, oh,
well, I want to show
-
the three most
significant digits
-
nd you're like,
okay, fine.
-
That shows me two
decimal places.
-
And you're like, success.
-
Problem is, is
that this isn't
-
going to work all the
time, most of the time,
-
a good chunk of
the time because
-
it works great if
you're counting
-
or if you remember that
you're counting from
-
that most
significant digit.
-
But what happens if the
-
most significant
digit is not three?
-
What if it's not
3.14 anymore?
-
What if it's 163.14?
You have a problem?
-
Because you only see
-
163 because it starts
-
counting from the most
significant digit.
-
The most significant
digit in
-
this example is now one.
-
I moved to the left.
-
You start the
counting from here.
-
So you see the
three numbers.
-
And so you're like,
-
where's my decimal places?
-
Well, if you wanted
-
to see your
decimal places,
-
you'd have to change
that to five.
-
But you have the same
problem because then if
-
the numbers to the left of
-
the decimal place
change again.
-
What if it becomes just
-
63.14159 and you still
-
only want to see the
two decimal places?
-
Well, this isn't going
-
to be a solution for you.
-
So what we have to do
then is we have to
-
introduce this
fixed manipulator.
-
So what we do is we
put in the fixed and
-
we send that along with
-
set precision
before the field
-
that we want to modify.
-
So now fixed
notation ensures
-
that you have
output that has
-
a decimal point that
has this format,
-
this decimal format,
this decimal notation,
-
this fixed point notation.
-
And then set
precision changes
-
the way it
behaves once you
-
use fixed and
that is that it
-
starts counting as
-
the most
significant digit,
-
the first decimal place.
-
So now, if we
change this to two,
-
then it starts counting
-
3.14 and ignores
everything
-
to the left of the
decimal place.
-
When it determines how
-
many digits it's
going to show,
-
you're going to
get everything to
-
the left of the
decimal place,
-
but you're only going
to get the first
-
two to the right of
the decimal place.
-
So let's see that.
-
So now you have 3.14
and it doesn't matter
-
how many digits you have
-
to the left of
the decimal.
-
You're still going to
guarantee that you only
-
see two decimal places.
-
See how that works?
-
Now, this is an error
-
that students of mine
make consistently.
-
They forget about how
this behavior works.
-
And so our textbook,
-
the first example
just shows you
-
how set precision
works like
-
I showed you at the
very beginning.
-
And there's a little
table in there that
-
shows you exactly
what I showed you,
-
but you have to read
further to learn
-
how fixed impacts things.
-
And so I think
students, I don't know,
-
maybe they don't
read that far,
-
they forget that part.
-
So if you want to control
-
how many decimal places
that you see, say,
-
four decimal places
of precision,
-
then you have to include
-
this fixed
operator as well.
-
Now notice that
it does round.
-
So 3.1415 became 3.1416
-
because that last decimal
-
place got rounded up.
-
Let me show that to you
one more time, 1416.
-
So for programming
assignments and stuff,
-
I give my students say,
-
okay, well, format two,
-
three decimal places
of precision.
-
This is the correct
way to do it,
-
just like this, using
-
fixed and set precision.
-
So let's go ahead and
-
see how Show Point works.
-
So I'll create
a variable X
-
and I'll assign
to it five.
-
Now, if I don't do any
formatting at all,
-
let's say that
I just display
-
the contents of X.
-
And even though X is a
floating point number,
-
we don't see
anything afterwards.
-
We don't see like 5.0
or anything like that.
-
Let's say that I wanted to
-
guarantee that
I saw a 0.0.
-
Well, that's
where the Show
-
Point will come into play.
-
So I can do Show Point X,
-
and then I compile
and run this.
-
And now you see 5.00000.
-
It fills out the
field length
-
with trailing zeros.
-
Now, if you want to
-
limit how many trailing
zeros that you see,
-
you can do that set
precision thing.
-
So you could do something
like set precision
-
and I only want to see
a total of two places.
-
So then you're going to
-
see the first two digits
-
because of how set
precision works.
-
Now, if you wanted to
-
see the couple of zeros,
-
then you could limit it
-
by having three there.
-
Of course, then
you're stuck
-
again with that same
kind of problem.
-
What happens if
you end up with
-
more digits than you
set set precision for?
-
Suddenly things
look really weird
-
and you end up with
this e-notation.
-
So the way to avoid
that is just use
-
your combination of
-
fixed and set
precision instead.
-
And if you do that,
then Show Point
-
becomes irrelevant.
-
So now you can see
that you've got
-
your 1505.000.
-
So Show Point
has its uses,
-
but if you use the
fixed at set precision,
-
then Show Point becomes
-
redundant or irrelevant.
-
Let me show you one
more thing about
-
how set precision
and fixed works.
-
These are set and
forget manipulators
-
which means that I only
-
have to have one
cout statement.
-
I only have to do
them one time.
-
So I could do
something like
-
set precision
two and fixed.
-
So in this case,
-
anything that comes
after is going to
-
be formatted according to
-
the set precision
two and fixed.
-
I only have to do
it the one time
-
and it's going to be
set until I change it.
-
So I could do something
then like cout pi,
-
and then cout X and
then cout 1995.
-
And all of these
fields, this field,
-
this field, and
this field are
-
all going to be formatted
according to this.
-
You only have to
do it the one
-
time until you
want to change it.
-
So what if I wanted
to reset the values?
-
Well, I can set
precision back to
-
its default by doing
cout set precision six.
-
Then I can do cout.unsetf,
-
and then inside here,
I do IOS fixed,
-
and then that turns
off the fixed.
-
So now, when you go
-
to show your number again,
-
you have that same
-
behavior that
you had before.
-
We used set
precision or fixed.
-
So you can see there's
the 8673.14 because
-
the precision
was set back and
-
we turned off that
fixed point notation.
-
So let's see how left
right and set W work.
-
So we'll have two fields.
-
The first field will
-
contain the
string hello and
-
then the second
will contain
-
the number 8675.309.
-
So this is the
first field,
-
the first thing that
we send to cout,
-
and this is the
second field,
-
the second thing that
we send to cout.
-
So let's take a
-
look at what the
output looks like.
-
You can see that
they all get
-
mushed together.
-
So the first thing
I want to do is I
-
don't want them to
be mushed together.
-
So what I'll do is I will
-
change the
default width of
-
the field and
the default is
-
eight characters and
that's the minimum.
-
So if you have more
than eight characters,
-
then C++ will
automatically
-
expand that field
width for you.
-
So you can see we see
the 19258675309, etc.
-
Now, I can play around
-
with how wide
that field is.
-
So if I do something
like this,
-
if I say set W 10.
-
Then the field that
hello occupies,
-
the amount of
characters that are
-
set aside to print out
-
the hello is going to be
-
at least 10
characters long.
-
So now, you'll see
the output change.
-
See how all of the
space occurred.
-
By default, the value
gets printed into
-
the field right justified.
-
So we've got 1, 2,
-
3, 4, 5 characters
in hello.
-
And so we were
given five spaces
-
worth of padding at
-
the very beginning to
-
fill out the entire field.
-
Now, this is not
-
set and forget like
set precision.
-
So set W is not
set and forget.
-
So if you want to change
-
the size of the
second field,
-
you have to include
set W again.
-
So let's make the size of
-
that field 20
characters wide.
-
So now we'll see what
that looks like.
-
So you can see, hello,
-
the first field went
-
right out to
where the O is.
-
That's ten
characters wide.
-
And then the
second field is
-
now 20 characters
wide and it
-
starts right after
the O and goes all
-
the way to the end where
the nine is because,
-
again, it's right
justified by default.
-
Now, we know how
set W works.
-
And let's see what left
-
does. You can
probably guess.
-
Now left has to do with
-
justifying the fields.
-
So where do you start
-
printing the value
within the field?
-
By default, it's
right justified,
-
but we can change that.
-
We can change it to
be left justified.
-
And keep in mind that
the manipulators
-
have to go before the
-
field you want
to manipulate.
-
So I want hello to
-
be printed in a field
-
that's ten
characters wide,
-
so I have to set W before
-
hello in the
cout statement,
-
and I want to be
left justified,
-
so I have to put that
below the hello.
-
So let's see how that
changes the output.
-
So now you can see
that the printing
-
begins in the left
part of the field.
-
And it's still 10
characters wide,
-
but you might
also notice that
-
the number here in
-
the second field was
also left justified.
-
So left and right are
also set and forget.
-
So you only have to
-
set them once and
everything that
-
follows is going to
adhere to that set.
-
So set precision,
fixed, Show Point,
-
left, right, are all
set and forget. Set W?
-
Not. Set W, you
have to use
-
for every field that
-
you want to change
the width for.
-
So if I want to make
the first field
-
left justified and
I want to make
-
the second field
right justified,
-
well, then I'll
just tell it.
-
So now this field
is going to be
-
subjected to these
two manipulators.
-
This field is
being subjected
-
to these two manipulators.
-
So that field was handled,
-
and then we want to change
-
things for this field.
-
So the set W and
-
the right are going
to affect field.
-
So you're going to see
-
how that changes things.
-
So now they're
way spread out.
-
So there's the hello
and its field was
-
padded with the
extra five spaces
-
and then there's
the number here,
-
and its field
was pre padded,
-
had leading spaces padded
-
in there to fill
out its field.
-
And you might be
thinking, well, that's
-
interesting. I
mean, that's great.
-
But why should I care?
-
Well, let me give you
-
a practical example here.
-
Let's say that I
wanted a table of
-
values and I wanted to
look a certain way.
-
So right now,
if I had say,
-
wanted to display a
table 8.161924 and I
-
wanted to display 123.456
-
and then on the next line,
-
I wanted to display
13.9 and 7.2453.
-
This is what it's
going to look like if
-
I don't do any
additional formatting.
-
Let's see how that looks.
-
It looks like garbage.
-
It's not what I was
hoping for at all.
-
Now, you might
say, okay, well,
-
let's improve
things a little bit
-
by putting a
space in between
-
the numbers and
you could do
-
that and it would make
-
a little bit of
a difference.
-
So we'll see what
that looks like.
-
So but it's still
not pretty.
-
What if I want to
align everything?
-
What if I want
everything to be
-
aligned by decimal places?
-
Well, if I want
-
there to be more
space, well,
-
maybe then you
think, oh, well,
-
I'll just do a tab
escape sequence.
-
We'll see what
that looks like.
-
It's still not
getting us there.
-
It's getting
closer because
-
you can see now that
-
at least we've got
the columns lined up.
-
If I put in
-
a couple of tab escape
sequences in between,
-
it's going to finally give
-
me something that
looks like columns.
-
But see how everything's
-
still left justified?
-
That's not going to
solve my problem for me.
-
So if we want to have
columns that are
-
nice and lined
up and let's
-
say we want to line up
the decimal places,
-
well, then we
can use set W
-
and then go back and use
-
our set precision and
-
fixed manipulators
to help us out here.
-
So rather than
do this double
-
escape sequence
thing here,
-
what we'll do
instead is we'll do
-
set W and that'll give
us better control.
-
So we'll make
each field, say,
-
10 characters
wide, so we'll put
-
that one there and
we'll put this here.
-
And this is going
to guarantee
-
that each field,
-
each column will be
-
a certain number of
characters wide.
-
So now let's see what
that looks like.
-
So we're getting closer,
-
but notice the
decimal places still
-
aren't lining up,
-
how are we going
to deal with that?
-
Well, we'll use
set precision
-
and fixed to help us out.
-
So we'll do cout
set precision
-
and let's say three
decimal places.
-
That's arbitrarily picking
-
that up off the
top of my head.
-
Use that fixed
point notation.
-
And then you can
-
see everything lines
up nice and pretty
-
because we ensured
that we had
-
enough characters
for each field and
-
then we use the
fixed point notation
-
and we said to three
-
decimal places
of precision.
-
So now everything lines
up nice and pretty.
-
Now, that's great,
-
but let me show you
another problem
-
that students
will run into.
-
So you might have
a situation where
-
you have to have a
label for your rows.
-
So you might have to do
something like this.
-
You might have to say, I
-
don't know, just
make up a word.
-
This is Row 1.
-
And then for
this second row,
-
you might do something
-
that looks a
little different.
-
So you might say Row 2.
-
You're thinking,
okay, cool.
-
I have to label
everything. My professor
-
wanted me to make
sure that each row
-
has a label of some kind
or the output has to
-
have a label,
and then crap.
-
So what went wrong?
-
Why is everything
messed up?
-
Everything's messed
up because the
-
first field,
-
we added a new field.
-
The first field of each
line isn't formatted.
-
So all it's doing
is just filling
-
out the field with
-
the value that you
want to display.
-
And so they're
different lengths.
-
And so those fields
are different lengths.
-
So the column of
-
the first fields now
is all messed up.
-
So you end up with
this jagged table.
-
So how are we
going to fix that?
-
Well, we're going
to remember to
-
format our first field.
-
So we want to
make sure that
-
that field is wide enough
-
to accommodate
-
the value that's going
to be put in it.
-
So I think that's
what 5, 6, 7,
-
8, 9, 10, 11, 12, 13, 14.
-
So that's 14 so we'll
give it an extra space.
-
So we'll do a field with a
-
15 for that first column.
-
So now, once we do that,
-
we will print it out
and you'll see that
-
everything is lined up
-
again as far as
the numbers go.
-
Now, you take a look at
this left side here.
-
It's weird
because remember,
-
by default, fields
are right justified.
-
Well, if I look at
this, what if I
-
want the output to be for
-
this first
column to be all
-
along the left
margin here?
-
Well, then I have to
change the justification
-
just for the first field.
-
So I'll make that
left justified,
-
then we'll see what
that looks like.
-
We're not going to
be done yet, but
-
I'll show you what
that looks like.
-
So now we've
got everything
-
fixed for that
first column,
-
but now
everything's messed
-
up in my other
two columns.
-
Why?
-
Because I made everything
left justified.
-
Remember, these are
set and forget.
-
So I need these values in
-
the other two fields
-
to go back to being
right justified.
-
So how am I going to
do that? Well, I'm
-
just going to tell
-
them to be right
justified.
-
So we'll do that and
we will do that.
-
So this first field
is going to be left
-
justified in a field
-
that's 15 characters wide.
-
And then the second field
-
here is going to be right
-
justified and it's going
-
to be 10 characters long,
-
and then this last field
-
is still going to
be right justified
-
when we'd set and
forget and it's going
-
to be 10 characters
wide as well.
-
And similarly for
the second row.
-
So now if run this,
-
it's going to look exactly
-
as I needed it to look.
-
So now you know how to use
-
IO manipulators to format
-
your output in C++ and
-
how to create nice
looking tables.
-
Thanks for watching.
We'll see you next time.