MyProgrammingLab12-2 Part 1 Transcript

Print

All right, let's take a look at 12.2. So I'm going to hit the plus sign. And I'm going to go to Week 1. I'm going to hit the plus sign. I'm going to go to Chapter 12, and hit the plus sign. And then right now we're looking at 12.2. And I'm going to click the first one. All right. 

So what they want is they want you to "Read five successive integers into the variables X1, X2," all the way to X5. And then, "Print on each one on its own line so that it's right justified with five digit width." 

So that means that each variable is going to be on its own line. And if it's five digits wide, it's going to take up the whole column. But if it's less than five digits, it's still going to take up the whole column, but it's going to be right justified, like that. 

The vertical bars are the wall, the left side, so you should ignore those. All right? So, let's make that happen. 

So the first thing is, to read five successive integers you just say "cin", and then you go into the first one, and then the second one, and then the third one, and then the fourth one, and then the fifth one. And that reads five successive integers. 

All right, so now, the next is we want to print. And let's take a look at this in C++, see how this looks. 

So it's saying that we have X1, X2, X3, X4, and X5 already created. So now it wants us to read five successive integers. So I'm going to say "cin", and I'm going to go into X1, X2, X3, X4, and then X5. All right, that's how we read five successive integers. 

Now, then it wants us to put everything into its own line. So I'm going to say "cout X1 endl" like that, and then "cout X2", and then "endl", and so forth. 

Now, what they also want is they want the column to be 5 wide. So how do I do that? Well, I come over here, and I hit the textbook, so right there's the textbook. Or I could also Google it because I love Google and you should be using Google a lot. 

And then we know that we're going to be in Chapter 12, 12.2. So I'm going to hit Chapter 12 and then we're going to hit 12.2. So right there is 12.2, right? So there's 12.2. 

Now, in this right here it says that if you want to have a certain number of decimal places, you want to use setprecision. And you want to use fixed if you do not want it to go into scientific notation.

So and then they show you some code to show you how this works. Now, one thing to make setprecision work is you must use the "iomanip" library. So you have to use this library right here. 

So now I'm going to go to the next page. And then you can see you put the word fixed first, and then you put the variable name. And then that writes the number. 

But if you wanted to have a certain number of precisions, and by putting fixed it prevents it from going into scientific notation. So with this right here if you want to have four decimal places you put "setprecision(4)", like that. This one's "setprecision(3)". So by setting the number of decimal places it comes out like that. You see? 

So this right here's "setprecision(3)". This is "setprecision(1)". All right? So that's how you set the precision. 

And then, on this right here, it says if you want columns you want to use "setw", set width. And with the set width they show you the code down here on how to make that happen. 

So we read from the file, and then we pop the data, okay? So on this right here they're writing to the file with this formatting. 

So you can see here set width is 8, like that. That's going to set the width to 8, so going across there's 8 for each one. So that's our "setw". 

Let's pop back over here. So what they're after is every single line, and that, actually I want to show you this in Visual Studio first. 

So that means that I want to have, and let me read what we're targeting again. So we want it to look like that, okay? So I'm going to pop back over here. 

So I want this to be a column width, so say "setw to 5", like that, "arrow arrow". Okay? 

Now, I can also set the right align or left align. So I want this to be right aligned. So I want to set right. If you want it to be left aligned you just type in the word left, like that. So I want this to be right aligned. 

Now if you don't set right, you notice in the textbook they did not set right or left, it will be right aligned by default, for numbers. 

So that would have worked if I had left it off completely, but I'm going to go ahead and just leave it on just to tell you all about the right and the left. 

Now, you'll notice that "setw" has blood. So how do we fix that? You have to come up here to the very top, and you have to include the "iomanip" library, like that. And that brings in the "setw" the "setprecision", it brings in all those items. 

So now this line right here should look really good. And I'm going to go ahead and do a Control-C. 

And actually before I finish it off, let's go ahead and run it because you want to test as you go. Write a little bit of code, test it, write a little bit more code, test it, like that. So you want to test as you go.

And then it wants to read five numbers, so I'm going to go ahead and put in 13, my lucky number, and then 25,511, so five digits wide, and then 123, and then 47, and then maybe 2. Okay? 

So one, two, three, four, five digits, successively. So when I hit the Enter key, C++ is going to read to the first space bar, and it's going to drop that into the first integer. Then it's going to read to the next space bar, and it's going to drop that into the next integer. Read into the next space bar, like that. Okay? 

So you can see that my 13 is in a column that is five wide, so that looks perfect. The other numbers were just, the X2 is actually this one right here, and that was put next. 

So what I'm going to do is I'm going to grab this one right here, this line, and it looked good so there's no reason for me to retype the whole thing. 

So instead I'm going to "copy pasta". Got to love my pasta. I love my copy pasta. All right, so you want to copy/paste, okay? 

And then also for copy/paste, you all should know if you highlight something, then do a Control-C, that will copy it to the clipboard. And then you go where you want it to end up. And then you do a Control-V, and that will paste. So Control-C, Control-V is the fastest way to copy pasta. 

So now I'm going to pop this to a 2. Then this is a 3, this is a 4, and then this is a 5. 

So now I execute. And then a 13; space bar; 7; space bar; 1, 2, 3, 4, 5; space bar; 4, 2, 1; and then 9, 9. All right? 

And you can see that my column widths are 5 all the way down. It's right aligned and it looks absolutely beautiful. So that's what they're after.

[End of Audio]