1 00:00:05,700 --> 00:00:09,700 In this video, we'll see some of the stream formatting options that are available that 2 00:00:09,700 --> 00:00:11,200 work with any type of data. 3 00:00:11,700 --> 00:00:13,900 These are very powerful and easy to use. 4 00:00:14,400 --> 00:00:17,300 They give you the ability to define a field with 5 00:00:17,300 --> 00:00:19,660 and then display the next data item, 6 00:00:19,660 --> 00:00:23,320 either left justified or right justified within that field. 7 00:00:24,120 --> 00:00:27,820 We can also specify the fill character that will be used to fill up 8 00:00:27,820 --> 00:00:29,220 any unused space. 9 00:00:30,320 --> 00:00:33,020 By default, there is no field with defined. 10 00:00:33,020 --> 00:00:37,620 And all output occurs right after the previous output as we've seen already in this course. 11 00:00:38,280 --> 00:00:39,880 If you do set a field width, 12 00:00:39,880 --> 00:00:43,880 then the next data item displayed will default to right justified 13 00:00:43,880 --> 00:00:45,380 within that field width. 14 00:00:46,180 --> 00:00:50,480 It's important to understand that if you set the field width and justification, 15 00:00:50,480 --> 00:00:54,580 they are only applied to the next data item placed on the output stream. 16 00:00:55,380 --> 00:00:59,180 When we set the fill character, that will persist until you change it. 17 00:00:59,180 --> 00:01:02,780 Again, this all might sound really complicated but it's pretty easy 18 00:01:02,780 --> 00:01:05,780 and a few examples will show you exactly how it works. 19 00:01:06,980 --> 00:01:08,640 But first let's look at the defaults. 20 00:01:08,640 --> 00:01:12,790 Here's an example that uses absolutely no stream manipulators or methods. 21 00:01:12,790 --> 00:01:16,040 This is the default behavior when you put data on a stream. 22 00:01:16,040 --> 00:01:18,640 As you can see, we're displaying the floating point number 23 00:01:18,640 --> 00:01:22,740 1234.5678 and then the string hello. 24 00:01:23,340 --> 00:01:27,440 The number will display with 6 digits of precision and it will round. 25 00:01:27,740 --> 00:01:30,740 And then hello is displayed immediately following the number. 26 00:01:31,840 --> 00:01:37,130 If we modify this program slightly so that it prints the number and the string each on a new line, 27 00:01:37,430 --> 00:01:39,630 then we get the number displayed as before 28 00:01:39,630 --> 00:01:43,780 on the left side with no padding up front followed by the string on the next line. 29 00:01:43,780 --> 00:01:47,140 So far this is what we've seen and it should be pretty clear. 30 00:01:48,740 --> 00:01:53,510 So now let's set the field width of the next data item placed on the stream using setw. 31 00:01:54,710 --> 00:01:58,370 In this case, we're using setw with a 10 as a parameter. 32 00:01:58,370 --> 00:02:02,470 This will create a field width of 10 for the next data item only. 33 00:02:02,470 --> 00:02:04,670 In this case, that data item is num. 34 00:02:05,220 --> 00:02:08,020 Notice that num will be displayed right justified 35 00:02:08,020 --> 00:02:10,020 by default in the field width of 10. 36 00:02:10,820 --> 00:02:14,620 I'm displaying a ruler of numbers just above the actual display 37 00:02:14,620 --> 00:02:16,220 so you can see the alignment. 38 00:02:16,880 --> 00:02:18,980 This ruler isn't displayed by the code. 39 00:02:18,980 --> 00:02:21,480 It's only there so you can better follow the examples. 40 00:02:21,980 --> 00:02:25,180 Notice that the last digit in number, the 7 in this case, 41 00:02:25,180 --> 00:02:27,380 is displayed in position 10. 42 00:02:27,380 --> 00:02:30,580 That's the rightmost side of the field width we set earlier. 43 00:02:31,080 --> 00:02:33,440 But now look what happens when we display hello. 44 00:02:33,440 --> 00:02:36,540 It displays right next to the number starting in column 11. 45 00:02:36,540 --> 00:02:40,640 That's because the field width and the justification only apply to the next 46 00:02:40,640 --> 00:02:44,640 data item placed on the stream. In this case, that data item was num. 47 00:02:45,300 --> 00:02:48,300 So hello has no field width associated with it, 48 00:02:48,300 --> 00:02:50,100 and it uses just the defaults. 49 00:02:51,980 --> 00:02:56,480 In this example, we set the field width of each of the 3 data items to 10. 50 00:02:56,480 --> 00:03:00,680 And remember, when we have a field width, the default justification is right. 51 00:03:01,040 --> 00:03:05,400 So in this case, num is displayed right justified in a width of 10. 52 00:03:05,400 --> 00:03:09,600 The first hello is displayed right justified also in a field of 10. 53 00:03:09,600 --> 00:03:13,260 And the last hello is also displayed right justified in the width of 10. 54 00:03:13,260 --> 00:03:16,360 And you can see from the display in the ruler exactly what's happening. 55 00:03:19,060 --> 00:03:21,360 This example shows how the field width only 56 00:03:21,360 --> 00:03:24,060 applies to the next data item put on the stream. 57 00:03:24,460 --> 00:03:27,230 In this case, we set a field width of 10, 58 00:03:27,230 --> 00:03:31,030 and we want left justification for the next data item, which is num, 59 00:03:31,030 --> 00:03:35,030 and we get what we expect. Num is displayed in a field width of 10, 60 00:03:35,030 --> 00:03:36,330 and it's left justified. 61 00:03:36,990 --> 00:03:38,990 Now we want to display hello. 62 00:03:38,990 --> 00:03:42,290 Notice that hello has no field width associated with it, 63 00:03:42,290 --> 00:03:44,690 so it will display starting in column 11. 64 00:03:47,190 --> 00:03:51,590 In this example, we'll set the field widths for all 3 items displayed. 65 00:03:51,590 --> 00:03:55,790 Num will have a field width of 10, and it will be right justified by default. 66 00:03:56,340 --> 00:03:59,140 The first hello will have a field width of 10, 67 00:03:59,140 --> 00:04:01,740 and we're explicitly setting right justification. 68 00:04:01,990 --> 00:04:06,090 We don't have to do this since it's the default, but we can do it to be very explicit. 69 00:04:06,750 --> 00:04:09,750 And the second hello will have a field width of 15, 70 00:04:09,750 --> 00:04:11,750 and it will also be right justified. 71 00:04:12,550 --> 00:04:15,210 You can see the output is exactly as we expect. 72 00:04:15,210 --> 00:04:18,410 Num is right justified in a field width of 10. 73 00:04:19,010 --> 00:04:22,070 The first hello is right justified in a field width of 10. 74 00:04:22,430 --> 00:04:26,090 And the final hello is right justified in a field width of 15. 75 00:04:28,290 --> 00:04:30,590 Now let's take a look at the setfill formatter. 76 00:04:31,250 --> 00:04:35,250 This only works when a data item has a field width associated with it. 77 00:04:35,750 --> 00:04:40,150 We supply the character that we want to fill any empty space in the given field width. 78 00:04:40,650 --> 00:04:42,250 Here's a simple example. 79 00:04:42,650 --> 00:04:46,860 We're setting the fill character to a dash with the setfill manipulator, 80 00:04:47,160 --> 00:04:49,160 and we're setting the field width to 10. 81 00:04:49,660 --> 00:04:53,660 This will apply only to num, which is the next data item on the stream. 82 00:04:54,160 --> 00:04:58,160 So we expect num to be right justified in a field width of 10, 83 00:04:58,360 --> 00:05:01,860 and the remaining space in that field to be filled in with dashes. 84 00:05:01,860 --> 00:05:06,060 That's exactly what we see. But what about the hello that we display next. 85 00:05:06,560 --> 00:05:10,860 We said that setfill was persistent, but we don't see any dashes with that hello. 86 00:05:11,360 --> 00:05:15,060 That's because that hello doesn't have a field width associated with it. 87 00:05:15,060 --> 00:05:18,720 So setfill only applies when we've set the field width. 88 00:05:20,920 --> 00:05:25,910 So let's look at 1 final example. In this case, we're setting the fill character to an asterisk. 89 00:05:26,560 --> 00:05:29,560 And then setting the field width of num to 10. 90 00:05:30,360 --> 00:05:34,360 We expect num to be displayed right justified in a field width of 10 91 00:05:34,360 --> 00:05:37,020 and asterisk to fill in the remaining space. 92 00:05:37,320 --> 00:05:40,320 And you can see by the output that that's exactly what we get. 93 00:05:40,720 --> 00:05:43,180 Now we set the fill character to a dash, 94 00:05:43,180 --> 00:05:45,950 and the field width to 10 for the first hello. 95 00:05:46,610 --> 00:05:50,810 We expect hello to display right justified in a field width of 10, 96 00:05:51,110 --> 00:05:55,210 and the blank space filled in with dashes, again, that's exactly what we get. 97 00:05:55,870 --> 00:05:59,270 Then finally, we set the field width of the last hello to 15. 98 00:05:59,770 --> 00:06:03,970 We expect hello to display right justified in a field width of 15. 99 00:06:04,220 --> 00:06:08,580 And since setfill is persistent and the last hello does have a field width, 100 00:06:08,580 --> 00:06:11,940 we expect that those blank spaces will be filled in with dashes. 101 00:06:11,940 --> 00:06:13,740 And as you can see, that's what we get. 102 00:06:14,840 --> 00:06:18,500 So you see it's really not that complicated, and the rules are very consistent. 103 00:06:18,500 --> 00:06:22,200 Using the formatting manipulators that we've seen in the last few videos, 104 00:06:22,200 --> 00:06:25,500 we can have a good amount of control over how our output looks. 105 00:06:25,860 --> 00:06:29,420 Now let's head over to the IDE, and we'll see these stream formatters in live code. 106 00:06:30,920 --> 00:06:35,120 Okay. So I'm back. I'm back in the IDE. I'm in the section 19 workspace, 107 00:06:35,480 --> 00:06:38,680 and I'm in the manip_fields project. 108 00:06:39,480 --> 00:06:43,980 And what we'll do here is we'll play around with the field with the justification and the setfill. 109 00:06:44,340 --> 00:06:48,140 You'll notice on line 7 here that I've got a function called ruler 110 00:06:48,140 --> 00:06:51,140 and all that function does is just display a line of numbers, 111 00:06:51,140 --> 00:06:54,340 and that way we can line up our columns easier when we see them. 112 00:06:54,340 --> 00:06:58,700 Okay. You don't have to do this, but it just makes it easier to learn exactly what's going on. 113 00:06:58,700 --> 00:07:01,200 And then I've got these 3 data items here that I'm using. 114 00:07:01,200 --> 00:07:04,500 I'm using num1, which is an integer and it's 1234. 115 00:07:04,900 --> 00:07:07,200 I've got num2, which is a double 116 00:07:07,200 --> 00:07:09,700 1234.5678, 117 00:07:09,700 --> 00:07:11,700 and just a simple string hello. 118 00:07:12,360 --> 00:07:16,360 The first thing we'll do is just like we did in the slides, let's just do the defaults. 119 00:07:16,360 --> 00:07:20,760 So all we're doing is we're putting num1, num2 and hello onto cout. 120 00:07:20,760 --> 00:07:23,960 And when we run that, you'll see everything just runs up together. 121 00:07:23,960 --> 00:07:28,220 So let me build and run. And then we see 1234, that's the integer, 122 00:07:28,220 --> 00:07:32,320 and then 1234.57, rounded 6 significant figures here, 123 00:07:32,320 --> 00:07:35,920 that's our precision, and then the hello. Everything jumbled up together, 124 00:07:35,920 --> 00:07:38,920 right. One right after the other, that's exactly what we expect. 125 00:07:39,420 --> 00:07:41,680 Okay. We can obviously -- 126 00:07:42,280 --> 00:07:45,080 let me comment this code out. We can obviously 127 00:07:45,080 --> 00:07:49,080 display one on each line. And sure, that'll help us read it a little nicer 128 00:07:49,080 --> 00:07:51,280 because now we'll see each one each line. 129 00:07:51,280 --> 00:07:53,880 But remember, the defaults are being applied. 130 00:07:53,880 --> 00:07:56,180 So we're getting 1234 as the integer, 131 00:07:56,180 --> 00:07:59,780 1234.57, which is rounding to the 6th 132 00:07:59,780 --> 00:08:02,380 precision for the digit and hello again. 133 00:08:03,180 --> 00:08:05,580 Now let's play around with the field widths. 134 00:08:06,880 --> 00:08:10,980 And here's a simple example. What we're doing is, again, we're displaying the ruler. 135 00:08:10,980 --> 00:08:15,080 And then we're setting the field width to 10 for the next 136 00:08:15,080 --> 00:08:19,080 data item that's being put on the stream, which happens to be num in this case right here. 137 00:08:19,080 --> 00:08:22,340 So num will be displayed in a width of 10. 138 00:08:22,340 --> 00:08:26,000 And if you recall num is that integer 1234. 139 00:08:26,000 --> 00:08:30,150 So that will be displayed in the width of 10, and the default is right justified. 140 00:08:30,150 --> 00:08:32,909 But then when num2 and hello are displayed, 141 00:08:32,909 --> 00:08:36,159 there is no field width associated with them. That field width 142 00:08:36,159 --> 00:08:38,360 only associates with the next item. 143 00:08:38,720 --> 00:08:42,380 So these 2 should print right next to each other and right next to num1. 144 00:08:42,380 --> 00:08:43,380 So let's take a look. 145 00:08:44,680 --> 00:08:48,180 And there you go. You can see 1234 right here. 146 00:08:48,180 --> 00:08:51,840 And let me grab my pencil, and I'll draw in white, I think it'll be easier to see. 147 00:08:52,440 --> 00:08:56,940 You can see right here the 1234, right there, that's our integer, 148 00:08:56,940 --> 00:09:00,190 and that's the 10th, right the 10th column. 149 00:09:00,190 --> 00:09:02,550 So it's being right justified in the 10th column. 150 00:09:02,550 --> 00:09:05,850 But then after that, we're seeing our double 151 00:09:05,850 --> 00:09:09,850 and our string, all printed run right next to the other, no formatting applied. 152 00:09:09,850 --> 00:09:13,210 Now let's do one more example. Actually let's do a few more examples. 153 00:09:13,710 --> 00:09:17,590 Let's do this one here. And the source code's available to you, 154 00:09:17,590 --> 00:09:21,690 I encourage you to go in there and play around and change things around. That's really the best way to learn it. 155 00:09:23,190 --> 00:09:26,090 Okay. So now look what we're doing. We're setting the width 156 00:09:26,090 --> 00:09:29,290 to 10 for num1 and for num2. 157 00:09:29,290 --> 00:09:33,190 So now we expect num1 to be right justified in a width of 10, 158 00:09:33,190 --> 00:09:35,890 num2 to be right justified in the width of 10, 159 00:09:35,890 --> 00:09:39,770 and then hello with no formatting, just put right on the end of num2. 160 00:09:39,770 --> 00:09:41,130 So let's run this. 161 00:09:42,930 --> 00:09:46,530 And here, down at the bottom, you can see that, that's exactly what happens here. 162 00:09:46,530 --> 00:09:51,030 There's my field width of 10. And there's 1234, right justified in there. 163 00:09:51,530 --> 00:09:54,230 Here's the other width of 10 right here the next field. 164 00:09:54,230 --> 00:09:58,590 And you see 1234.57, right justified in there as well. 165 00:09:58,590 --> 00:10:00,090 And there's my hello. 166 00:10:00,890 --> 00:10:04,890 I'm going to put a couple of new lines at the end, just so that 167 00:10:05,390 --> 00:10:07,990 it'll separate that time elapsed message a little bit, 168 00:10:07,990 --> 00:10:12,650 so it'll be a little bit easier to see what's going on. And there they are. I just had them commented out. 169 00:10:12,650 --> 00:10:14,650 So I'll uncomment them. 170 00:10:15,750 --> 00:10:18,410 Okay. So let's take a look at this example now. 171 00:10:19,610 --> 00:10:21,310 I will uncomment this code. 172 00:10:21,970 --> 00:10:25,670 And notice what's happening here, I'm setting the field width to 10 173 00:10:25,670 --> 00:10:27,470 for each one of those items. 174 00:10:27,470 --> 00:10:30,170 By default, we're going to right justify everything. 175 00:10:30,170 --> 00:10:35,160 So num1, num2 and hello will each display right justified in a field width of 10. 176 00:10:35,960 --> 00:10:39,960 And let's give that a run. And there you go. You can see 1234 the integer 177 00:10:39,960 --> 00:10:43,620 in the field width of 10, 1234.57 178 00:10:43,620 --> 00:10:45,280 in its own field width of 10. 179 00:10:45,280 --> 00:10:49,280 And hello, you can see the 0 right there, right. That would be the 30th column, 180 00:10:49,280 --> 00:10:52,480 the third field width of 10, and it's right justified. 181 00:10:52,980 --> 00:10:55,340 Comment this out and see what this is doing. 182 00:10:55,340 --> 00:10:58,220 Now this is doing exactly the same thing except 183 00:10:58,580 --> 00:11:00,880 we're left justifying each one of these now. 184 00:11:00,880 --> 00:11:04,880 Remember, we have we want to left justify then we have to say it explicitly, 185 00:11:04,880 --> 00:11:07,240 right justified is is the default. 186 00:11:07,240 --> 00:11:11,600 I always like to put in left and right that way whoever's reading my code afterward 187 00:11:11,600 --> 00:11:13,800 understands exactly what's going on. 188 00:11:13,800 --> 00:11:16,000 Even though, we know it's the default 189 00:11:16,000 --> 00:11:18,660 somebody might have forgotten, so it's nice to see that there. 190 00:11:18,660 --> 00:11:22,020 So here width of 10, left justified num1, 191 00:11:22,020 --> 00:11:26,320 width of 10 left justified num2, and width of 10, left justified hello. 192 00:11:26,680 --> 00:11:27,980 So let's give that a run. 193 00:11:28,530 --> 00:11:30,230 And you'll see the difference between 194 00:11:30,230 --> 00:11:34,690 this guy right here where they were right justified and now down here where they're left justified. 195 00:11:34,690 --> 00:11:38,050 So you can see that 1234 is displaying in column 1, 196 00:11:38,050 --> 00:11:41,050 1234.57 in column 11, 197 00:11:41,300 --> 00:11:44,700 and hello in column 21, 198 00:11:45,600 --> 00:11:49,260 just like we expect. Okay. So now let's do a couple with the setfill, 199 00:11:49,660 --> 00:11:54,360 and I'll comment this out, we'll walk through this in a minute. So notice what I'm doing here, I'm setting 200 00:11:54,360 --> 00:11:56,960 the fill character to the dash. 201 00:11:57,320 --> 00:12:00,920 Now this is a persistent setting. This will persist until I change it. 202 00:12:01,320 --> 00:12:05,320 And what I'm doing here is I'm setting the width to 10 203 00:12:05,320 --> 00:12:07,320 left justified num1. 204 00:12:07,320 --> 00:12:09,570 Now num1 was only 4 digits, right. 205 00:12:09,570 --> 00:12:12,870 So the rest of the space in that field of 10 is going to be 206 00:12:12,870 --> 00:12:15,870 padded or filled with that dash character. 207 00:12:16,530 --> 00:12:19,130 And the same thing will happen to these guys. 208 00:12:19,330 --> 00:12:23,990 Remember, if we don't have a width set, then setfill doesn't apply. 209 00:12:23,990 --> 00:12:27,790 It only applies when we're setting the field width. So in this case, we expect to see 210 00:12:27,790 --> 00:12:32,390 all 3 of these guys. Left justified in a field width of 10. 211 00:12:32,390 --> 00:12:34,690 And all the spaces that are left over, 212 00:12:34,690 --> 00:12:38,680 all the blank spaces in that field of 10 filled with dashes. So let's take a look. 213 00:12:40,180 --> 00:12:41,730 And there you can see right down here. 214 00:12:41,730 --> 00:12:45,720 You can see 1234, and then dashes all the way to the 10th position. 215 00:12:45,720 --> 00:12:50,080 And then 1234.57 dashes all the way to the 20th position. 216 00:12:50,080 --> 00:12:52,980 And then hello and dashes all the way to the 30th position. 217 00:12:53,880 --> 00:12:57,030 Okay. Had we right justified them the dashes would have been on the left. 218 00:12:57,030 --> 00:12:59,590 It would have filled any empty space in that field width. 219 00:13:00,990 --> 00:13:03,590 And the last example is 220 00:13:04,090 --> 00:13:06,890 we're doing now is we're using different setfill characters. 221 00:13:06,890 --> 00:13:10,290 So we're setting the filled width to 10 for each one of them again, 222 00:13:10,290 --> 00:13:13,890 and we're left justifying. And in this first example for num, 223 00:13:14,040 --> 00:13:17,540 I'm using the asterisk as the fill character. For num2, 224 00:13:17,540 --> 00:13:20,540 I'm using the hash mark or the pound symbol. 225 00:13:21,040 --> 00:13:23,140 And for hello, I'm using the dash. 226 00:13:23,140 --> 00:13:26,740 We should see 3 different fill characters filling up all that space. 227 00:13:26,740 --> 00:13:29,200 Remember, once this statement is done, 228 00:13:29,200 --> 00:13:32,800 this is the fill character that's set, so that's going to persist 229 00:13:32,800 --> 00:13:35,800 until I change it otherwise. All right. So let's run this. 230 00:13:36,900 --> 00:13:40,800 And there you can see that I'm using the asterisks for the first field, 231 00:13:40,800 --> 00:13:44,050 the pound signs or the hash symbols for the second field 232 00:13:44,050 --> 00:13:46,550 and the dashes for the final field. 233 00:13:46,800 --> 00:13:49,680 Okay. So that's it for this video. In the next video, 234 00:13:49,680 --> 00:13:53,980 we've got a challenge where we're going to put some of this stuff together to display a table. 235 00:13:53,980 --> 00:13:58,340 Now you get a chance to play with them and put them together and understand how you can use 236 00:13:58,340 --> 00:14:02,700 your precision, your left, your right, your fixed and so forth all together 237 00:14:02,700 --> 00:14:05,900 to create a table of tour prices for trips to South America. 238 00:14:05,900 --> 00:14:09,900 So that's the challenge coming up in the next video.