C++ Formatting
The naming page left clear left and right, a total with plus, and the printed lines left=10, right=2, and 12 from box.cpp. This page stays in the Quality and Wrap-up module on purpose. The workshop file on disk stays box.cpp so you keep one familiar build path. The new work names formatting: spacing and layout that make a small program easier to scan. Keep the same names and the same plus. Put braces on their own lines. Indent the body of main with four spaces. Keep spaces around = and +. The screen still shows left=10, then right=2, then 12. Compiling waits for the next page.
Formatting here means how the source looks on disk, not a new calculation. Reading a brace on its own line is reading a clear block edge. Reading four spaces before each statement in main is reading one indent step. Reading spaces around = and + is reading a calmer expression. Keep #include <iostream> so the prints work. Do not invent a compiling lesson on this page.
Programs use tidy layout when a crowded one-line body still runs but hides mistakes. You do not need a new workshop file name. The build command stays familiar: one source file box.cpp, one output name box, one run that prints three lines. You compile only box.cpp again for this workshop file on disk.
Think of formatting as neat shelves for the same tools. The previous page chose clearer labels. This page chooses clearer spacing. Do not expect compiling as a required keyword in the checked file. The printed left=10, right=2, and 12 through tidy layout is the whole lesson point.
Above main, keep only the include. Write int main() then an opening brace on the next line. Inside main, indent each statement four spaces: int left = 10;, int right = 2;, the labeled prints, int total = left + right;, the total print, and return 0;. Close with a brace on its own line. Keep #include <iostream> at the top. Do not crush the body onto one line in the checked file. Do not invent a compiling demo on this page.
Walk the run carefully from the first line of main. The program starts at main. It stores ten in left and two in right. The first print shows left=10. The second print shows right=2. The total uses plus, so twelve is stored. The third print shows 12. Then main returns and the program ends. There are three printed lines on the screen: left=10, then right=2, then 12. The checked file never keeps a one-line crushed body.
Say the steps out loud once while you read the file. Open main with a brace on its own line. Indent each statement. Keep spaces around equals and plus. Close the brace. Return. That short story is the whole workshop path. Nothing else belongs in the checked body. No new math. No compiling keyword. When the screen shows left=10, right=2, and 12, the story matched the source.
If you temporarily crush main onto one hard-to-read line, the program can still print 12. That failed experiment stays in the prose as proof that layout is for readers. The checked file keeps tidy braces and indent.
If you change layout but leave a minus in total, the last line is still 8. Formatting does not fix a wrong operator. The checked file keeps plus so the last line is 12.
A common mistake is to mix tabs and spaces until the file looks crooked. Another common mistake is to invent the word compiling as a required keyword here. A third mistake is to remove all spaces around operators. A fourth mistake is to wait for cin. A fifth mistake is to change the numbers while cleaning layout.
This page stops before the next tool, where you look again at how the build command turns source into a runnable program. The next page names that tool once at the end. The formatting habit you have just written only shows tidy layout on the same sum. Keep the printed 12 so the order stays clear for a first careful reading of the file.
Remember that formatting changes appearance, not arithmetic. You still need plus to finish. After a successful run the screen shows left=10, right=2, and 12. After you undo a bad experiment the checked file returns to that tidy path. Stability of the checked file matters as much as learning the layout habit itself.
Open box.cpp. Keep left, right, and total. Put braces on their own lines. Indent the body of main with four spaces. Keep spaces around = and +. Do not add a compiling lecture inside the file. The program file name on disk stays box.cpp. The checked file is below, and the new habit is formatting with tidy layout.
#include <iostream>
int main()
{
int left = 10;
int right = 2;
std::cout << "left=" << left << std::endl;
std::cout << "right=" << right << std::endl;
int total = left + right;
std::cout << total << std::endl;
return 0;
}
Rebuild with g++ -std=c++17 box.cpp -o box, then run ./box. If g++ is missing and clang++ is the compiler you installed, swap only the tool name: clang++ -std=c++17 box.cpp -o box. The screen shows left=10, then right=2, then 12.
Check the three printed lines first. The table gives each row its piece, so compare your run with it line by line before you move on to experiments. If the last line is 8, total still uses minus. If the body is crushed onto one line, restore the tidy braces before you trust the formatting path. If old muddy names return, put left and right back.
📊 formatting layout and what the screen shows
| Piece | What the screen shows |
|---|---|
| First | left=10, because the first labeled print runs |
| Last | 12, because total still uses plus |
| Rule | tidy braces and indent help readers; plus still makes 12 |
| Screen | left=10 then right=2 then 12 |
Try one change at a time. Temporarily crush the body of main onto one long line, rebuild, and see that 12 can still appear while the file is harder to read. Restore the tidy braces and indent. Next, temporarily print ready instead of total, rebuild, see ready, then restore the total print. Each experiment changes one thing on purpose, so you see either messy layout or a different third line. Undo each change before the next one starts, so the file always returns to the checked workshop version with tidy layout and 12 once again after every single careful try back on disk again.
This page ends the formatting tool and stops before the next one. The next page is compiling, which names the build step that turns box.cpp into a runnable program again. The formatting habit you have just written only shows tidy braces and indent on left and right from box.cpp while total still prints 12. You leave with left=10, right=2, and 12 on the screen. Count the printed lines before you leave. There are exactly three printed lines on that final screen, and those lines are left=10 then right=2 then 12 alone right there now after the final successful build run on disk today now after the tidy-layout check.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top