UPI Study › Courses › C++ Programming › Free lessons › Lesson 9.10 Free with a free account
Applied Challenge: Student Record System
C++ Programming · Chapter 9: Structs, Classes, and File StreamsSection 9.10 · lesson 68 of 84video coming soon
← File Read and Write OperationsNext lesson: Non-Sequential File Access →
In this lesson
Building a complete program with classes.
- Decompose a brief into classes and operations
- Put each rule in exactly one place
- Print an aligned report, and save and load records
- Keep main to the menu, and test the edges
Transcript
Everything said in the video, section by section (1,592 words). Read along, or search it.
Chapter Capstone
Everything Used at Once
Everything in this chapter now gets used at once. A class with its own rules, a collection of them, operations to add, find, change and remove, a report worth printing, and records that survive after the program closes. It's built as a real program is built: across several files, with the design decided before the typing starts. Attempt each piece yourself before reading the solution.
The Brief
Institute Records and Required Operations
Here's the brief. An institute keeps records of its learners. Each holds a roll number, a name, a course, and a mark out of 100, and roll numbers are unique. Staff need to add a record, look one up, change a mark, remove a record, and print a report with grades, the count, the average, and the highest scorer. And records must survive between runs.
Decomposition
Nouns Become Types, Verbs Operations
Take it apart the way you've learned. Nouns become candidate types, and verbs become operations. The record becomes a student class, because it has rules of its own: a mark must be in range, and a grade follows from it. Roll numbers are unique is a rule the collection enforces, because no single record can check it. Add, find, change, remove and report are questions about the whole set, so they belong to a roster class. And surviving between runs means save and load.
Design
Two Types Across Five Files
There are two types, and for each, the interface is settled before the fields. A student knows its roll number, name, course and mark. It reports each, reports its grade, validates a new mark, and converts itself to and from a line of text. A roster knows how many students it holds, and which. It adds one, rejecting a duplicate roll, finds, updates, removes, prints the report, saves and loads. That's spread across five files. Each class has a header saying what it is, and a source file saying how it works. Main dot cpp holds the menu, and nothing else. Nothing includes a cpp file, and main never sees how anything works.
Student Header
Constructors, Accessors, and Private Data
Here's the student header. There's an empty constructor, and one taking a roll, a name, a course, and marks. Then read-only accessors for each value, and a set marks function that returns false if the mark is out of 0 to 100. Grade returns a letter, and to line converts the record to one line for the file. The data members are private: the roll number, the full name, the course name, and the score.
Student Rules
One Validation Rule in One Place
The source file is where the rules live. The full constructor starts the score at zero, then calls set marks. So there's one validation rule, in one place. Set marks refuses anything below 0 or above 100, and leaves the old value alone. Otherwise it stores the new mark, and returns true. Grade gives A at 90 and above, B at 75, C at 60, D at 40, and F below that.
Design Tension
Constructors Cannot Report Refusal
This meets a real design tension early. The constructor calls set marks, which may refuse. And a constructor has no straightforward way to report that refusal back to the caller. Here, the record is simply left at zero, which is safe, but silent. There are three honest responses. Validate before constructing, so the invalid case never reaches the constructor. That's what main does here. Provide a separate check the caller can use first. Or use the language's error-reporting machinery, which you haven't met yet. It's a genuine limitation, not an oversight. What matters at this stage is recognizing it, rather than pretending the constructor caught everything.
Roster Class
Find, Add, Update, and Remove
Now the roster. Find by roll loops over the students, and returns the position of the one with that roll, or minus 1, which isn't a valid index. Add first checks whether the roll is already present. If it is, it returns false. That's the uniqueness rule. Otherwise it pushes the student. Update marks finds the student, and returns false if absent. Otherwise, it hands the new mark to the student's own set marks. And remove finds the student, and erases it from the vector.
Design Review
Three Choices Worth Noticing
Three things in that listing are worth stopping on. Every operation returns a bool saying whether it worked, so the caller is never left guessing. Find by roll is written once, and used by all three of the others, so the uniqueness rule and the lookup rule can't drift apart. And update marks doesn't check the range itself. It passes the value to the student, which owns that rule. Duplicating the check would mean two places to change, and one of them eventually forgotten.
Sample Report
Aligned Rows, Summary, and Empty Case
The report prints headings for the roll, name, course, marks and grade, then a rule line, then one aligned row per student. Text is left-aligned, and numbers right-aligned, with marks fixed to one decimal place. Priya Nair scores 88, a B. Rahul Kumar scores 94.5, an A. Meena Iyer scores 37, an F. The summary line gives 3 records, an average of 73.2, and the highest scorer, Rahul Kumar. The duplicate roll and the mark of 101 were refused, as the first two lines show. And the empty case is handled. With no records, the report prints no records, rather than an average of nothing.
Persistence
Saving with a Vertical Bar Separator
Records are saved one per line, with fields separated by a character that can't appear inside a field. A comma is the obvious choice, and a poor one, because names contain commas. A vertical bar is safer. And the choice must be recorded, because the reader and writer have to agree. Save to opens an output stream, returns false if it couldn't open, and writes each student's line. Load from opens an input stream, and returns false if it couldn't. Then it clears the roster, so loading replaces rather than appends. Then it reads each line, and keeps every student that parses. A bad line is skipped, not fatal. Here's a small file saved by an earlier session. Two records, one per line, with the fields split by vertical bars.
File Failures
Surviving Missing Files and Damaged Lines
The file layer must survive several failures. If the file doesn't exist, that's normal on a first run. Report it, and carry on with an empty roster. If it can't be written, return false, so the caller can warn the user rather than losing the day's work silently. A line with the wrong number of fields is skipped, keeping the rest. One damaged record shouldn't discard the file. A mark out of range in the file is refused by set marks, so the record loads with zero. Reporting the line number would be better. And a blank line at the end produces one field, so the field-count check rejects it. Here is a hand-damaged file: one good record, one broken line, another good record, and a blank line. The report shows the two good ones. The broken line and the blank one were skipped, and nothing else was lost.
Main Program
A Menu and Nothing Else
Main should contain the menu, and nothing else. It loads from students dot txt, where an absent file is fine. Then it loops until the user chooses 6, to quit. For choice 1, it reads a roll, a name, a course and marks. It validates the marks before constructing, and reports a duplicate roll if add refuses. Choice 5 prints the report, and the remaining options follow the same shape. At the end, it saves, and warns if saving failed. Every line of logic belongs in one of the two classes. If main starts calculating averages, or searching vectors, the boundary has slipped.
Graded Exercise
Three Tiers of Extensions
Now the graded exercise. Build the system in full, then complete the extensions for your tier. Tier 1 completes the build: finish find, update and delete, reporting clearly when a roll is absent, and add a report for a single course. Tier 2 extends it: sorted reports, using comparators rather than separate report functions, a grade distribution chart, and a loader that reports the line number of every skipped line. Tier 3 is the challenge: several subject marks per student, with a variable number of fields in the file. And undo, using a structure you specified earlier in this chapter. Marks weigh function most, then the separation of class, logic and menu, then validation, report quality, and readability.
Test Checklist
Boundaries, Duplicates, and Damaged Lines
Test every submission against these. A first run with no file starts empty, without a message that looks like a failure. A report with no records prints no records. A duplicate roll number is rejected, with the existing record left untouched. Marks of minus 5, 0, 100, and 101: 0 and 100 accepted, and the others refused. The boundaries are the point. Finding or deleting an absent roll is reported clearly, and nothing else changes. Deleting the only record, then reporting, behaves exactly like the empty case. Save, quit, restart and report gives an identical report. And a hand-damaged line in the file is skipped, while every other record still loads. The runs in this video showed three of them: the duplicate roll, the mark of 101, and the damaged line.
Quick Check
Where a 40-Student Limit Belongs
Quick check. A new rule arrives: a course may hold at most 40 students. Does it belong in Student, in Roster, or in main? Pause and think. In the roster. Like uniqueness, it depends on every record at once. A student knows only itself, and main holds only the menu, so the roster's add checks the count.
Key Takeaways
Types, Single Rules, Reported Outcomes
To summarize. Nouns in a brief become types, and verbs become operations. Each rule lives with the data it governs, in exactly one place. Every operation reports success or failure. Save with a separator the data can't contain, and survive damaged lines when loading. And main is only the menu.
About C++ Programming at UPI Study
C++ Programming (Computer Science 242) is a self-paced online course of 84 video lessons in 11 chapters, from your first program to decisions, loops, arrays, functions, classes, files, pointers and inheritance. It carries a NCCRS credit recommendation for 3 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus Every course with PRO →
Why take it here
Why students take C++ Programming with UPI Study
Where the credit goes
Universities that work with NCCRS
C++ Programming carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About Applied Challenge: Student Record System
How do I decide which class a rule belongs in when designing a student record system in C++?
Why should a C++ student record program use a vertical bar instead of a comma to separate fields in a file?
How should a C++ program load records from a file that is missing or has damaged lines?
Why can't a C++ constructor report that a mark was invalid, and what can I do about it?
About the C++ Programming course
How many college credits is C++ Programming?
How much does C++ Programming cost and what is included?
Which universities accept UPI Study credits?
How long does C++ Programming take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 3 credits in C++ Programming.
84 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
3 credits · NCCRSEnroll →