BETA Try the new lesson view
Code

Clear names and braces

The program works correctly. What's left is to make it clear.

Rename with Shift+F6

Click on a (anywhere) and press Shift+F6 (Refactor → Rename). Type grade and Enter: CLion changes every place that variable is used at once. Likewise s → sum, n → count.

Changing them by hand, it is easy to miss one place, and then the compiler reports 'a' undeclared.

By hand

  • { } around both if/else branches.
  • sum = sum + grade; → sum += grade;
  • #include as in the other topics: the standard library first, a blank line, then our input.h. The blank line splits them into two blocks, and Reformat Code no longer swaps them.

Ctrl+Alt+L once more: nothing changes now, the file already matches the style.

Run it

8, 9, 10, 0 → Average: 9.00. The program behaves the same as after the fix, only now you can read it at first glance.

Commit: Use clear names.