You are looking at lesson 5. Your place is still lesson 4, task 1: Style.
The precondition and the postcondition, assert and its message, Release mode, and what doesn't go into an assert.
A function is an agreement between its author and whoever calls it:
x is not negative";Write the contract as a comment above the function:
/* Rounds to a whole number.
Before: x >= 0.
After: the nearest whole number; x.5 is rounded up. */
int round_to_int(double x) {
The precondition isn't made up: in topic 2 you saw that (int)(x + 0.5) rounds a negative number wrongly.
Nobody checks a comment. assert does:
#include <assert.h>
int round_to_int(double x) {
assert(x >= 0);
return (int)(x + 0.5);
}
While the condition is true, assert does nothing. When it is false, the program stops at once and says where. For example, if the program of step 4 called round_to_int(-2.7):
Assertion failed: x >= 0, file C:/.../practice/05/main.c, line 56
Process finished with exit code 3
So the bug is caught where it started, not a hundred lines later, when the wrong number has already spread through the whole program.
A user typing 11 is not a bug in the program: you have to expect it, and in topic 3 you asked again with while. assert is for another case: when the code calls a function with something it can't accept, e.g. you forgot to check the grade before print_in_words(11).
| What is wrong | How to handle it |
|---|---|
| the user typed an invalid value | while and ask again |
| the code broke a function's precondition | assert stops the program |
assert also works for tests: a function that calls your functions with known data and checks the result.
void run_tests(void) {
assert(round_to_int(0) == 0);
assert(round_to_int(7.49) == 7);
assert(round_to_int(7.5) == 8);
}
What to test:
is_valid(10) and !is_valid(11).When a test fails, assert prints the line that is false, so you see straight away which case doesn't work.
CMake's Release configuration defines NDEBUG, and then every assert disappears from the program. So never put inside an assert something that has to happen:
assert(read_int("How many? ") > 0); /* in Release the question is gone */
The course's tests are always compiled with assert switched on.
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.