Hello. Welcome to the section 19 challenge 4 solution video.
I'm in the section 19 workspace in the challenge_4_solution project.
And here's the solution to this project. It's pretty straightforward.
We're going to do it a couple different ways, like I said. One, we'll put line numbers on every line,
and then we'll modify it a little bit so that we're only putting line numbers on lines that actually have text.
So we've got our includes. We're including iostream, fstream iomanip and string.
And we're creating our input stream right here, our input file,
which is romeoandjuliet.text.
And of course, it's in my parent directory because again I'm in CodeLite.
I'm creating an output file stream where we're going to create
a copy of this file with line numbers,
and I just called it romeoandjuliet_out.txt.
I'm opening both files. And I'm double checking that they're both open
for reading and writing. If they're not, I provide an error, just like we've seen before.
Here I've got a line which is a std string.
And I've got a counter for the line numbers, and I've initialized it to 0,
pretty simple.
That's it. Now I'm going to start processing the file.
I'm going to use getline because I want to read a whole line at a time.
I'm using getline, and I'm reading a line from infile.
What I'm doing is no matter what if it's successful,
I'm I'm
And I'm simply displaying line number, left justified and a width of 7
that gives me a nice formatted
area on the left side of the file that's got all the line numbers in there
and then line,
which should also be left justified against that eighth position, basically.
That's it. Then I display copy complete, close both files, and we're done.
So if we run this
copy complete, we can go to our project and open up the folder.
And we should have a romeoandjuliet_out, which we do right here, I'll double click on that.
And there you go. You can see everything's lined up nicely with line numbers on the left-hand side.
Okay. Now what I said earlier was I -- this kind of bugs me
all these line numbers on these blank lines. So i don't want that.
Let's just do another version of this program that gets rid of those.
So that's what we'll do next.
What we'll do is we need the logic right in here.
Basically, what we want to do is we want to do this
only if the line is not empty, right.
So let's put an if statement in here. We'll say if the line we just read is empty,
then what do we want to do, then we just want to write to the file.
We still need to write the blank line to the file. So we'll just say infile --
sorry, not infile, outfile
and we'll write an endline.
That's it. Then we'll have an else statement here.
And we'll wrap all of this up
inside that else. And we'll just indent it over.
Okay. That's it. So now we're only displaying a new line.
We're not displaying text because there is no text, right.
We're not displaying a line number because we don't want to display a line number on an empty line.
And if it is not an empty line, then we're going to treat it as normal.
We're going to increment the line number and display it. So let's run that.
And let's take a look at the output again.
And that's pretty much what I wanted,
right. All the blank lines are still displayed, but they are not
numbered.
And that's it. I hope your challenge was fun.
And I hope you basically got the same kind of solution.
There's a lot of different ways that you can modify this program.
If you had any and want to share, please post on the forums.
So that's it for this video.
There are a couple videos left in this section. The next one is going to be
about using string streams,
which allows us to use our own in-memory strings
as streams which is pretty cool.
And we will address input validation,
which is a question that's come up a bunch of times in the forums.