MyProgrammingLab Programming Challenge 70036 Part 2 Transcript

Print

So now I'm going to go ahead and just say "cout". And we've got to get that exact sentence, otherwise CodeLab will mark it wrong. All right, so I'm going to grab this exact sentence here. All right, so I'm going to do a Control-C. And this one right here says "input", and this one says "output". All right, cool. Now I'm going to do a Control-V. All right. 

So, "Enter in the file name," and then I'm going to say, "string". Input file, and you can call variables whatever you want. So I'm calling mine input file, but you can call it anything that you want. Then, I'm going to use getline, right? Because we use getline to get strings. And get it from where? From the console. And put it where? Put it into our input file variable. All right, and there's my first one. 

Now, getline, you can see, is bleeding. If I hover my mouse over it, it says it has no clue what I'm talking about. Getline is undefined. And I need to come up here to the top, and I need to bring in the string library. All right? So for getline to work, you must have the string library as one of your libraries. All right? 

So now I want to get the second one, so I'm going to grab these three lines of code. As software developers, we copy/paste a lot. So you want to get real comfortable with copy/pasting. And then here I'm going to change this to output, like that. And then the file name, again, I'm going to just use output. And then I'm going to double click on this, and then I'm going to do a Control-C. I'm going to double click over here, and then I'm going to do a Control-V. So when I paste, if it's highlighted, it will replace it. Okay? So there's getting the input from the user. 

And then what I want to do is open the file to read. So I'm going to go ahead and say, "input file string infile". And this right here must be input file string, because that's the name of the class. This right here is a reference name. It can be anything that you want. So I'm going to use "infile". And the programming standard is to make your variable names and your reference names make sense as to what the content is. So with this right here, you know that this is my input file name, because of how I named it. And this is my output file name, and then this is my infile string, just based on how I named it. So it makes sense with what's in there. 

So now I'm going to go ahead and say, "input file stream", and then I'm going to use the input file. So I'm going to say, "input file". Like that. And then I'm going to say, "if the infile is open", so we're able to open, "if that is false". So that means that, we tried to open, but it did not open. Okay, if that's true, then we had an issue. 

Now I'm looking at my in file and the blood is not going away. So I'm going to hover my mouse over it, and it says, "Incomplete type is not allowed." All right? So I'm going to come up here to the top, and I'm going to say, pound include "fstream". Because remember we need to have fstream to read and write to files. Okay? So now the blood down here goes away. Perfect. 

So now I say, "Is the file open?" And if that's false, so it did not open, then I've got a problem. So I'm going to go ahead and say, "Error, unable to open the file." And then I'm going to show you the file name, so the person can then start doing some troubleshooting and figure out why. Okay? Then I'm going to pause so the person who's running my application can read the error before it disappears. And then I'm going to return a -1. And the -1 is universal for error. So I'm going to return that to the environment, so that if a person is launching multiple components, they know that this component failed. All right? 

So now, if everything goes well, we're going to come down here to the next line. And then I'm going to create an output, let's see. You know what? I think I want to ask for the name down here. So I'm going to grab this, and I'm going to pop it over here. And the reason I'm thinking I'm going to go this way is because I'm going to ask for the input file name, and then I'm going to try to open it. And if I cannot open the input file name, then there's really no reason to ask for the output file name. Does that make sense? So if we fail here, then I'm not even going to ask for the output file name. 

So I'm going to get the input file name, I'm going to do my best to open it, then I'm going to get the output file name. So get output file and open it. And let's see, I think I'm going to change my comment. Get input file name and open it. All right, so this comment is going to describe this block of code. And then this comment is going to describe this block of code. And I'm going to do a little bit more to actually open the output file stream. 

So you want to have one comment per block of code. If you have one comment per line of code, that's way too much. So you want to have a balance. You want to have a good balance. Good commenting without over commenting. All right? 

So here I'm going to open up my output file stream, and I'm going to call it "outfile". And this is going to be using the output file name. Like that. And remember, if you don't put the iOS App, then it defaults to the output type, which destroys the content that's sitting in the file. And that's good. That's what I want to happen. So if the file already exists, I want the content to be wiped out, and only have the new content of me fixing the sentences file. 

So now, the next thing then, is to see if it opened. Now I'm going to use a little bit different technique on this one. I'm going to say, "if outfile dot fail". So we tried to open it, but it failed. All right, so you can see that this right here is a little bit different technique than this right here. But they both work. So I'm going to go ahead and leave this. And I could use this technique again down here. No problem. Or I could use this technique up here as well. And they would be fantastic. 

So one of the things I love about being a software developer is there are multiple ways to get the job done. And as long as it works, and as long as it is efficient, then it is the correct answer. So that's the two things that you've got to accomplish. It's got to work properly, and it has to be efficient. All right? 

So if it failed, curly brace enter key, then I'm going to do the exact same thing I did here. So I'm going to grab this code right here, and I'm going to do a Control-C on it to copy. I'm going to come down here, and I'm going to do a Control-V. Only this time, watch what I do. I'm going to double click on the file name. Okay, so I'm going to do a Control-C, and then I'm going to do a Control-V right there. 

And you know what, I'm not liking outfile versus output file. That doesn't really tell me what's actually happening. Okay? So I'm going to make another adjustment. I'm going to change this to "input file name". And then here, I'm going to change this to "input file name". And then here, it's going to be input file name. So now, if I tell you, "Input file name," you know exactly what's happening. Okay? So I like that naming convention a lot better, so that's what I'm going to use. 

All right, so this right here is going to the exact same thing. "Output file name." Now I'm going to double click on this, I'm going to do a Control-C, then I'm going to do a Control-V, Control-V, and Control-V. All right? So I love my copy/paste. So far, so good. I think we're good. 

So now, the next thing that I want to do, is we're going to come down here, and we're going to say, and actually, I've written quite a bit of code, so let's go ahead and test it. See how we're doing. I'm going to run it. And then for the file name, I'm just going to type "x". And then it says, "Error, unable to open the file called x. Press any key to continue." Oh, okay. So now I'm going to run it again, and this time I'm going to type in "sentences dot txt", because that's what we named our file. And now the output file name. See, it did not crash this time. Or it did not give me the error message this time. So then here, I'm just going to say, "fixed dot txt". And everything's working. So I did not get any errors. All right. So far so good. 

Now, let's come on down, and then we are looking for the first character so we can capitalize it. Because remember, we want to go through and find the first character. Now, the way that I'm going to do this, and you can do this a lot of different ways, but this is the way that I'm going to do it. I'm going to create a flag, and I'm going to call it, "looking for first character". And I'm going to set it to "true". So a Boolean is oftentimes called a flag. And since we are looking for the first character right now, we are opening up the file, and I'm expecting the first character to be right away. Okay, so I'm going to set my flag to true. 

And then I'm going to come down and start looking into the file. So for the file, I'm going to say, "while infile dot peek is greater than -1". I'm going to start reading every single thing. All right, so what the peek command does for us, is it looks at the first character, and then it says, here is the ASCII value. And if it gives me anything greater than 0, or greater than -1, then we have more data. So I'm going to start reading, and reading, and reading. All right, so if it hits the file and there's no data in there, it's going to give me a -1. So if it gives me a -1, that's an error. That means that there's no more data. I'm done.

[End of Audio]