< Return to Video

Mastering C++ Manipulators: setprecision, left, right, setw, fixed, and showpoint [6]

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

more » « less
Video Language:
English
Duration:
20:07

English subtitles

Revisions Compare revisions