You are looking at lesson 11. Your place is still lesson 6, task 1: Arrays.
hw/11-gradebook: error_text, save_students and load_students with error codes; main loads gradebook.txt, adds students and saves; it doesn't overwrite a broken file.
Checklist
The gradebook, version 5: students no longer disappear. At the start the program loads gradebook.txt, lets you add new ones, and at the end saves everything.
input.h, input.c – put your own from hw/10-gradebook here.
gradebook.h – replace the first part with yours from hw/10-gradebook. The new part is written: GRADEBOOK_FILE ("gradebook.txt"), Error and three functions with contracts.
typedef enum {
GB_OK = 0,
GB_CANNOT_OPEN,
GB_BAD_FORMAT,
GB_TOO_MANY,
GB_WRITE_FAILED
} Error;
gradebook.c – copy your functions from hw/10-gradebook and write the new ones:
| function | what it does |
|---|---|
error_text(e) |
ok, cannot open the file, bad line format, too many students, write failed |
save_students(path, s, n) |
each student is a line Ona Petraite: 8 10 7; checks fopen, fprintf, fclose |
load_students(path, s, max, &n, &line_no) |
reads lines with fgets; a bad line → GB_BAD_FORMAT and its number line_no; more than max → GB_TOO_MANY |
A good line: the name up to the first : (is_name returns 1, it fits in MAX_NAME), after it 1 to MAX_GRADES grades from 1 to 10, separated by spaces. An empty line, 8,5, 11, x – a bad format. An empty file – GB_OK and 0 students.
main.c:
Students loaded from gradebook.txt: 2
How many new students? 1
Student 1 of 1
Full name: Agne Kazlauskaite
...
Students by name:
Agne Kazlauskaite (A. K.), average 9.00: 9 9
Jonas Jonaitis (J. J.), average 6.50: 6 7
Ona Petraite (O. P.), average 8.33: 8 10 7
Best average: Agne Kazlauskaite (9.00)
Students saved to gradebook.txt: 3
GB_CANNOT_OPEN) – No gradebook.txt yet: starting an empty gradebook. and carry on as with 0 students.Error: gradebook.txt, line 2: bad line format., The gradebook is unchanged. and return EXIT_FAILURE;: don't overwrite a broken file.New students must be between 0 and 8.); when there are none – The gradebook is full.Best average line.save_students fails – Error: gradebook.txt: write failed. and EXIT_FAILURE.gradebook.txt appears in the folder the program runs in: with ▶, the build folder (build/debug or cmake-build-debug).
ctest --preset debug -L hw11
hw11_functions – unit_tests.c: save and load, an empty file, no file, ten bad lines (each one on line 2), too many students. While the functions are empty:
Assertion failed: nearly_equal(average(grades, 5), 8.8), file .../hw/11-gradebook/unit_tests.c, line 36
hw11_precondition – save_students(..., -1) must stop at your assert.
hw11_gradebook_1–3 – the whole program, each in its own folder: no file; a file with two students and invalid numbers; a broken file. Both the output and what was written to gradebook.txt are checked (in test 3 – that it is unchanged).
hw11_input, hw11_doxygen, hw11_errors, hw11_main – input.c is yours, gradebook.h has Doxygen and Error, gradebook.c uses assert(, fopen(, fclose(, fgets( and doesn't use fscanf( or while (!feof(, main.c calls load_students(, save_students(, error_text( and checks GB_CANNOT_OPEN; no TODO anywhere.
Commit and push. Actions → Points: HW 11 (Gradebook with a file), 10 points when all 9 tests pass.
Each one ticks itself when you do it.
All six done. You know your way around.
It is laid out like CLion: the same panels in the same places. This tour shows each one, and you can reopen it any time with Tour.
Switches the left panel: Lesson plan, Project files, Changes. Click the active icon again to hide the panel.
In CLion: the tool window bar runs down the left edge, too.
This part is not on this task: it appears when a task needs it.
Every task in this lesson. Now marks your place. Clicking a task only lets you look; it never moves your place. Project files and Changes open here too.
In CLion: this spot holds the Project view (the file tree).
This part is not on this task: it appears when a task needs it.
Opens the file for your task by itself. A tab appears for each file the step opens. The breadcrumbs above the code show the folder.
In CLion: the same editor tabs and breadcrumbs.
This part is not on this task: it appears when a task needs it.
Diff shows what changed, like git diff: red is the old code, green the new.
In CLion: the diff view in the Commit window looks the same.
This part is not on this task: it appears when a task needs it.
Terminal for build commands, Database to see your tables. They open by themselves when a task needs them. Drag the top edge to resize.
In CLion: Terminal, and the Database tool window.
This part is not on this task: it appears when a task needs it.
What to do now, right above the code, so you never miss it. Numbered notes in the code explain the important lines. Details shows what you should see and a hint.
In CLion: no equivalent: this is your lecturer's guide.
This part is not on this task: it appears when a task needs it.
Done is the only button that moves you forward. Look back lets you review without losing your place.
This part is not on this task: it appears when a task needs it.
Guided, Focus code or Floating. You can drag the panel edges too. Your layout is remembered for next time.
In CLion: you can also rearrange and hide tool windows.
This part is not on this task: it appears when a task needs it.
Six small missions. Each one ticks itself when you do it, so you know you have found everything.