MyProgrammingLab Programming Challenge 70036 Part 4 Transcript

Print

So for the next thing, I would probably get rid-- I would probably make sure there's only one or two spaces after the period. And that means that this right here would disappear, and it would look even better. 

But that's it for the programming challenge. So now that I've got this working, and it's looking really good to me. I want to do a Control A to select my code. And then I'm going to do a Control C to copy. I'm going to go back over here to the CodeLab. I'm going to click down here. I'm going to do a Control V to paste. 

And it's going to crash because of two things, and the first is I put my pauses. And the CodeLab does not like the pauses in there. And the second, if you remember, I said to open the file, you have to put your .c_str, right? But I'm going to go ahead and run this, so you can see what I'm talking about with these messages that I put here. 

So I'm going to hit Submit. And it says Compiler Errors. And it says-- let's see right here. OK. In the function main, there's no matching function call for in file input stream. And then it puts a little arrow right there. Do you see that? And then you start looking a little bit further. It gives you some more information. And here it says you need to have the open mode. But that's actually not the case. You don't have to that. 

And I'm checking to see if there's any way-- and then you can see here, it doesn't like the system pause. Do you see that? It says system was not declared in this scope. So those two things, it's not liking. So I'm going to pop back over to my work area. And then my pause, that was for me, so I can see-- so I can read the errors like that and still have my debugging because I love the debugger. 

And then down here, I'm going to get rid of this system pause as well. And then, down here at the very bottom, I also have a system pause, so I'm going to get rid of that. 

Now, the other thing was-- and let me go ahead and hit Submit again. And it cleans it up a little bit. But you can see that it's saying now right here on your input file stream, we're having a problem with that right there. So it's saying it doesn't like having just a string on here because what it actually needs, it needs to have a constant character pointer. Do you see that right there? 

So it's saying, in reality, it needs to have a constant character pointer and not a string, and we're giving it a string. And for Visual Studio 2010 and Visual Studio 2012, that was the case. 2013 and 2015 are now OK being given a string, as you saw, because our application in Visual Studio 2015 works. 

But let's deal with it. No big deal. So that's why I put this note here. So we're going to come down here. And let me show you what it looks like in Visual Studio. So I come over here in Visual Studio, and this is a string. And then I go after it, and I put .c_-- and then there's the str. And you can see that it gives back a constant character pointer. Do you see that? 

So it takes that string, and it converts it to a constant character pointer. And it's a method, so I have to put in my left paren and right paren, like that. So-- oops! I put it in the wrong place, didn't I? I'm going to grab this, and I'm going to move it down here because that's where it actually needs to go because this is my end file, this is where I'm opening up my pipe, and that's where I need to convert the string to a constant character pointer. 

And then this line, I'm going to grab this line. I'm going to do a Control C on it. I'm going to flip over to my CodeLab, and then right here is where I open the pipe. Now, we do a Control V on it. So I'm taking the file name. I'm converting it to a constant character pointer. And now that's going to work. 

But then I've got another one right down here. So again, over here, this is where I open up my output file stream. So my string, I say constant character pointer. And you can see that's going to give us back that constant character pointer here. So now, I'm going to grab this line. I'm going to do a Control C on it. I'm going to pop over here to my CodeLab. I'm going to grab this line here. 

And if I wanted to, I can actually click on here and I could actually go .c_str left paren, right paren and just type it in easy enough. I like my ID. I like having Visual Studio help me out, so I do use it quite a bit. So right now, if I run it, you can see we're golden. We're correct. 

Now, one thing for you all to know is this was just my first swipe at this. There's a lot of different ways that we could have done this. We could have done this with methods. We could have just a lot of different ways we could have done this. Like, for example, this right here I could have done this with a method right here because this is basically the same as this. 

I could have also processed it using a method, different things like this. And then also, here on the character, I-- well, on this part right here, I showed you that I could do it this way with is open. I could do it this way with is fail. And then, here, if you're saying the file is not open, what a lot of people will do in production code is they won't actually-- oops! How did I do that? Let me get back in. 

Hopefully, my code is still there. If it's not, it's no big deal because I've got it in Visual Studio. So here they will not type it like this. And I'm going to hit my Backspace. Instead, you can actually say, if not open. So you can just put a not in front of it like that. So this is the way you'll actually see it in production code, so I'm going to go ahead and change this. 

And I want to remind everybody that the exclamation means not. So if the file is not open, then I'm going to run this code right here. So get comfortable with this because this is-- like I said, this is what you'll see in production code. 

And then another thing, on this one right here, I'm actually using this line here to check and see if the file is-- still has data. Now, another way I could have handled this is, before my loop, I could have created a character variable like this. And I think I want to do this in Visual Studio just to make sure I don't have any typos. So here, I'm going to change this. 

Again, you will not see this in production code. Typically, people just say if not, file is open like that. So I'm going to go ahead and say exclamation means not just to remind everybody. And then down here, I could have also done this. So here I check and see if there's more data. But you can actually check for more data while you're reading. 

So I can actually say Character C, And I'm going to do the before my loop because scope is curly brace to curly brace. And so I'm going to pop it before here. And then here, inside of the loop, I'm going to say C is going to be a sign in file.get and if C is not equal to a negative 1, then that means that I've got a character. 

And actually, you know what? This will not work because if C is a character, then, right away, it gets converted to the character itself, and we lose that negative 1. So I would actually have to do this. I would actually have to say int file character because it would have to hold the Ascii value. So then I would say file character, and then I read from the file. And if it's not equal to negative 1, then I have data. 

Then, I would have to type cast the file character, which is an int, into a regular character that I can then start doing my comparisons with. So the important part is, here, I'm actually doing my read. And I'm doing my read as part of the while statement. And some people think that this is really cool, and if you like it, go for. And the point that I want to make is that there's multiple ways to do this. 

Let me make sure that everything works and make sure it works this way. So I'm going to go ahead and delete my file, my fixed.txt. Now, I'm going to pop over here. I'm going to run this again, and then sentences.txt, and then fixed.txt. 

Let's see if it worked. Going to pop over here. Ah, it did not work. So let's take a look at this. So actually, I probably want to do this first. I want to get the character off the file first. So to make sure that that happens first, I'm just going to throw a parentheses around it. So now I'm going to pull the character out of the file first. Then, I'm going to compare it to negative 1. And if it's at the end of the file, the get will get a negative 1. 

So let's see. This should work. Let's see this right here. We get-- we have system is ambiguous. This just appeared. So if I look at system it says system is ambiguous. If you get that error where it says system is ambiguous, or sometimes, it will say cout is ambiguous like that-- just come over here to your I/O stream, slash it out, do a build, rebuild, and then come back and put the I/O stream back in, and then do a build, rebuild. You can see now system is fine. 

And Visual Studio does that every now and again. No big deal. That's how you get rid of that. So I'm going to come back over here and we're going to look at this again. The first thing I want to do is read the character. So I'm going to put that in parentheses. And then I'm going to say if it's not equal to negative 1, then I want to convert that character using its Ascii value into a character, or you convert that int, the Ascii number, into a character and then put it to my c. 

So I'm going to execute, see if this works. And that is sentences.txt and then fixed.txt. I'll flip over here. There's fixed. And that's perfect, Absolutely fantastic. 

So the important part that I want to get across is there's multiple ways to do this, and some solutions are fantastic. Some solutions are good. But this one-- this is one way. This is one option. And if you did it a little bit differently, that's OK too. Just make sure that you logically understand it.

[End of Audio]