Welcome back to the section 19 challenge 2 solution.
I hope you had fun doing that challenge.
It was pretty nice and you've got a lot of different ways that you can tweak it and extend it to make it your own.
So I'm in the section 19 workspace in the challenge_2_solution project.
And I wrote some functions to solve this.
But first, I just want to go through the logic in the main so you can see that it's
really dead simple and the functions that I wrote make it even simpler.
So let's just start here at the top
and we'll walk through this. I've got an input file stream called infile,
that's what I'm going to use to read the responses file.
I've got a string for the answer key.
And I've got a string for the student name and the student responses that I'm going to read
in each iteration of the file read.
Okay. Then I've got a running sum
and a total number of students that we're going to use to calculate the average score.
Okay. Pretty simple. So let me scroll up just a little bit here.
And here's my program pretty much.
I'm opening the input file right here responses.txt.
Notice again that the dot dot slash is because I'm in CodeLite.
If you're in another IDE, you'll probably have to do the same thing or just use responses
.text it's totally up to the IDE.
If we can't open that file, we display problem opening file and we're out of here just like before.
And then the very first thing I'm going to do right here I'm reading that first line in the file,
right, or the first string in the file which is the answer key.
That's it. I'm reading it from infile, storing it in an answer key, so that will be that abcde
string. Then I'm going to print the header,
which just displays student
and score
with a bunch of dashes underneath it. So that's a function that I'm using to print the header.
And then I've got the logic of my loop right here. So I'm saying while
read from infile, the name and the response pair.
If anything and those two are strings that I declared up top, I just showed them to you a minute ago.
If anything it all goes wrong here, if we hit under file,
if we have a bad read, if we've got bad data, this will fail and I'm out of here.
Okay. But if we don't, now we've got name and response
contain the strings for, for example, Frank and Frank's response.
We increment the total number of students because we need to do that to calculate the average.
And then I'm going to call a function called process response. I'm going to pass into it
the response and the answer key. And it's going to return an integer with the score.
So it'll return
012345 in this case because that's you can get them all wrong or
you can get them all right or everything in between.
That's it. I add the score to my running sum of scores.
And then I call print student.
I'm going to call that each time. So I'm going to print the student's name. So right here, it'll say Frank,
and Frank got a 5.
Then I loop and I do it again. Okay. So I'm going to clear this screen real quick.
Then at the very end, I just calculate the average.
I'm going to be sure that I don't do a division by 0 error.
So if the total number of students is not equal to 0, I'm going to do the division.
And I'm doing the static cast that you guys have seen before here.
So I need to make one of these a double.
And at the end, I print the footer.
And in order to print the footer, it doesn't know what the average score is. So I'm sending it to it.
I close the file, and I'm done.
Okay. So you can see the logic is really straightforward, right.
You can see here I'm opening the file.
I'm making sure it's open. I'm closing the file down here.
I'm reading from the file right here and here as well.
And the rest is just the processing that I need to solve this specific problem.
Okay. So now let's take a look at these methods. I'll scroll up a little bit.
And let's start with the print header method,
pretty straightforward, right, just a bunch of print statements to cout.
And what I'm doing here is I'm displaying the student, I'm displaying the score.
And I'm using the set w manipulator here and left to just line them up nicely.
I could have used constants for 15 and 5 and 20,
but I chose not to with such a simple little program,
and then I'm using setfill to display the little dashed line.
That's it. I've setfill back to space just in case somebody else needs it.
I don't want to have them display dashes. The footer
is passed in the average score,
and it just displays the footer. Really, really straightforward again.
Okay. Now I probably should have used the set precision and set fixed
one here, but I'm using it here. So it's it's persistent, but
good practice is to put it in here as well, right about here.
That way if someone changed it and then this function got called,
you'll set it the way you want it.
And then the last one is the print student
where I pass in the student's name and the student score and I just display them,
just like we've done before. So these are just these handy little functions.
And the reason we're using these functions is so we don't clutter up our main.
And this is just a lot of code a lot of noise really
because it's just all about -- I mean all I'm doing here is displaying student, displaying score.
But you've got all this other stuff around it with the manipulators I need to set
widths and I need to do left and I need to write precisions.
And it just really clutters up the logic in the main.
So you really want to
extract that out and modularize it out into a function get it out of the way.
And out of sight, out of mind, it's abstraction.
I just call footer whenever I need it and pass it on average.
I just call print header, and that's it, real simple.
And then the last thing is that function that actually calculates the number, right.
So that's process response right here.
So we're passing in the student's response, which was a string that
could be like abcxx or whatever it is.
And the answer key, which in this case was abcde.
And we're setting the score to 0. We're assuming they haven't gotten anything right.
And all we're doing is we're looping one character at a time through that answer key.
And we're comparing the answer key at I
to the response of the student at I. If they're the same, we're bumping up the score.
We're doing that throughout, so we're going to check this guy with this guy
and then this guy with this guy and so forth.
When we're done, we'll return the score.
So that's it. That's the solution or that's one solution.
You may have your own solution. If it's -- you think it's cool,
post it on the forum. And if we run this,
that's what we get, just what we expect.
Students Frank Larry Moe Curly and Michael, their scores
and then the average score 3.6. And the nice thing about this solution,
I think, is this right here.
I mean I'm looking at the code right here,
which is really doing most of the work well this first
one as well, which is reading the key. So this this piece of code right here.
This is really what's doing it all. And when you read it, it's really, really easy to read.
I'm reading the answer key, I'm printing the header
while I'm grabbing those pairs. I'm processing the response.
I'm printing each student, and then I'm calculating the average and printing it in the footer.
So looking at this, it's really easy to understand it, to debug it.
And that's it. That's the challenge. I hope you had fun with it. I hope you extended it.
And there's another challenge coming up right after this one.