C++ Naming
The debugging page left labeled x and y, a fixed plus sum, and the printed lines x=10, y=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 naming: clear identifiers a reader can say again. Rename x to left and y to right. Rename sum to total. Keep the plus. Print left= and right= with the values, then print total. The last number stays 12. Formatting waits for the next page.
Naming here means choosing spellings that say what a slot holds, without changing the math. Reading int left = 10; is reading the first stored number under a clearer name. Reading int right = 2; is reading the second stored number. Reading int total = left + right; is reading the same plus under a clearer sum name. Keep #include <iostream> so the prints work. Do not invent a formatting rule on this page.
Programs use clear names when a short letter still adds but hides the story. 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 naming as sticky-note labels on the same values. The previous page showed a flashlight on a wrong operator. This page shows clearer labels on a correct plus. Do not expect formatting in the checked file. The printed left=10, right=2, and 12 through clear names is the whole lesson point.
Above main, keep only the include. Inside main, write int left = 10;, int right = 2;, then print left= with the value, then print right= with the value, then int total = left + right;, then print total, then return 0;. Keep #include <iostream> at the top. Do not keep the old x and y spellings in the checked file. Do not invent a formatting 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 the muddy one-letter names.
Say the steps out loud once while you read the file. Name left and right. Print both with labels. Add them into total. Print total. Return. That short story is the whole workshop path. Nothing else belongs in the checked body. No x local. No formatting keyword. When the screen shows left=10, right=2, and 12, the story matched the source.
If you keep int a = 10; and int b = 2; with labels that still say x=, the names and labels disagree. That failed experiment stays in the prose. The checked file uses matching left and right labels.
If you rename the variables but leave a minus in total, the last line is still 8. Clear names do not fix a wrong operator. The checked file keeps plus so the last line is 12.
A common mistake is to treat a different capital letter as the same name. Another common mistake is to invent the word formatting as a required keyword here. A third mistake is to start a name with a digit. A fourth mistake is to wait for cin. A fifth mistake is to change the numbers while renaming.
This page stops before the next tool, where spacing and layout make a small program easier to scan. The next page names that tool once at the end. The naming habit you have just written only shows clearer identifiers on the same sum. Keep the printed 12 so the order stays clear for a first careful reading of the file.
Remember that naming changes spellings, 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 left and right path. Stability of the checked file matters as much as learning the clear-name habit itself.
Open box.cpp. Rename x to left and y to right. Rename sum to total. Update the labels to left= and right=. Keep total = left + right. Do not add a formatting rule. The program file name on disk stays box.cpp. The checked file is below, and the new habit is naming with clear identifiers.
#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 labels still say x=, the rename is incomplete. If the last line is 8, the total still uses minus. If old one-letter names remain, replace them before you trust the naming path.
📊 naming left right 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 | clear names label the same values; plus still makes 12 |
| Screen | left=10 then right=2 then 12 |
Try one change at a time. Temporarily rename left back to x but keep the left= label, rebuild, and see the mismatch in your reading even if the number still prints. Restore left. 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 a muddy name or a different third line. Undo each change before the next one starts, so the file always returns to the checked workshop version with left, right, total, and 12 once again after every single careful try back on disk again.
This page ends the naming tool and stops before the next one. The next page is formatting, which names spacing and layout in a small program. The naming habit you have just written only shows clear 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 clear-name 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