image
Read Data and Print Statistics Project

Contents

Introduction
1
Course Overview
PREVIEW1m 37s
File Input and Output
2
Conclusion

The course is part of this learning path

Start course
Difficulty
Intermediate
Duration
1h 15m
Students
33
Ratings
5/5
starstarstarstarstar
Description

This course covers file input/output (file I/O). Being able to read from and write to secondary storage is important because often we want to keep some data persistently. By the end of this course, you will have the skills necessary to do this.

Learning Objectives

  • Understand the fundamentals of both file input and file output and how to use both in tandem
  • Use stream manipulators to make the structure of the data more organized
  • Use dynamic memory, pointers, and classes to make more complex and interesting applications

Intended Audience

  • Beginner coders, new to C++
  • Developers looking to upskill by adding C++ to their CV
  • College students and anyone studying C++

Prerequisites

To get the most out of this course, you should have a basic understanding of the fundamentals of C++.

Transcript

In this section, we've learned the fundamental skills needed to work with file input and file output. In this lecture, you'll work on the section's first project by reading in a file named scores.txt, which is provided for you as a resource with this lecture. You will use the data which should contain integer values zero through 100, and you will write to the console the minimum score, maximum score, and average score. Your program should reject invalid input, which for our purposes will be any integer value less than zero and any value more than 100. So, it should neither count to them nor consider them when determining the minimum or maximum scores. So, you should create a project called FileStats, and copy the scores.txt file into the project directory underneath the created solution, or in the appropriate area for your IDE. Then, code the project, so that it prints out the requested statistics for the input data, ignoring invalid data, of course. As a big hint, related to the maximum and minimum values, you can set each of these two the first value you read from the file, and then change it whenever you find a larger value for the maximum or smaller value for the minimum. Okay, so this should give you an idea of what the file stats program looks like. Debug, start without debugging rather, and we have statistics, max is 97 and min 22 rather, and then average is 68.2963. So, if you use the same input file that I'm using, then you should get something like this, so this is something like the output should look. So, hopefully that helps. So, pause the video and come back when you're done or if you need some help. How'd that go for you? Were you able to complete this project? Let's do it together. So, first let's create the new project, and we'll call it FileStats. Now, I have the repos open and also where the data was that I downloaded. From the resources, we have scores.txt, which if you open it, you can see there's a bunch of images. There are some invalid out of range. Don't delete them. That's not the way to solve the problem. Although, it would make your coding life easier. You should write the program that can handle invalid input, that's part of the challenge. So, let's look for FileStats, okay? Right there, and I will copy the scores.txt over there. Good, all right. So, now let's create our source file main.cpp, and fill in the skeleton. Good. If stream infile.open scores.txt and then later infile.close. Excellent. We'll make the sum a double because when we create the average variable we're going to need to do a division, and if we have two integers that will do integer division. So, we could either cast one at that time or we could just use one of them as a double or make one of them double, and the count doesn't make sense to make a double at all, but the sum we can tolerate that. Count = zero, to start with. We will assume max and min are zero to start with, but we will fix that in a moment. Actually, it probably makes sense, make that 0.0 also, to be correct. And to make sure that the file actually exists, if it doesn't, we'll say error opening file bailing out, and then return 1. All right. Next up, we're going to do a little looping here, so while !infile.eof. And then we know that afterwards we're going to do some statistics printing to the console in this case. We probably want to make sure that the count is not still zero because there will be a division going on. Average is equal to sum divided by count, whatever the sum is by that point from the loop, and then print out the statistics. And we could use the formatting libraries, but we'll just keep it fairly simple, and of course the average. All right. It looks good to me. Now the mid of the program will be in the while loop. All right, so here's what we do. Every time, every iteration through the while loop we have infile, and then that goes into our input number that we're using for the sole purpose of getting a value from the file, right? One at a time. So, I said that if you're on the first iteration, we can set the minimum and maximum to that first element you read from the file, and then you update them when you find something that is contrary to it. So, let's do this. We'll say, if the count is equal to zero, even though this is a sentinel controlled loop, we're still keeping track of account, we'll talk about that in a second. So that we know how to find the average, we have to know how many items we read in that were valid. So, at the very beginning, we just set the minimum or maximum to the first number, because we don't know any better, as far as we know when we first read them those are the minimum and maximum. But if at any point, we find an input number that is less than the minimum, then we want to set min equal to that because that's obviously the new minimum. Equivalently, we're in a similar version if we find an input number bigger than the maximum, the new max equals that input number. And then we have to remember we're trying to keep track of stuff for the average, so sum += inputNum and then also count ++. All right. So, we've taken care of, it looks like I'm missing something here. So, it looks like I forgot to put in the check of the range, so that's what we'll do here. We'll say if the inputNum greater than or equal to zero and inputNum less than or equal to 100. Otherwise, we're going to get some invalid data. So, I need to put all of this right in here, and we're going to tap it in and organize it a little bit. That's better. Okay, so that make sure that we're even in range, if we're not, it just goes down to the bottom and then up to the next iteration, it skips the number if it's not within this range. So, no special action has taken other than not considering the value. We aren't even increments the counter if it's an invalid value because that would change our average. All right. Looks pretty good to me. So, let's run this. See what we get. All right, and of course, we get some values here, 97, 22, obviously it ignored the outliers like over 100 or less than this 22, obviously less than zero in this case. So, good job. All right. Hopefully, you found this project challenging enough to help you practice and expand your knowledge and skills, but not too overwhelming. In the next lecture, you will work on another project. This time, we will once again revisit our rectangle class and also use dynamic creation of objects on the heap like we've done before. But the information you will read will be from file this time, so I'll see you there.

 

About the Author
Students
1379
Courses
20
Learning Paths
4

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics