7.x — Chapter 7 summary and quiz
Quick review#
- Compile errors stop the build. Logic errors make wrong results.
- Fix the first error message first. Check the line above, too.
- Turn on warnings and fix them all.
- Print values or use a debugger with breakpoints to see what's happening.
- When stuck: make it smaller, explain it, take a break, then ask.
Pick an answer. You find out right away if it is right.
Your program compiles and runs, but prints the wrong answer. What kind of error is that?
It built fine, so the compiler was happy. The code just doesn't do what you meant.
The compiler shows 25 errors. Which should you fix first?
One early mistake can cause lots of errors below it. Fix the first, recompile, and many others often vanish.
What is a breakpoint?
A breakpoint pauses the program at that line so you can look at the values.
Is each one a compile error or a logic error?
- Forgetting a
; - Using
<=instead of<so a loop runs one too many times - Misspelling a variable name
- Using
=instead of==in anif
- Compile error
- Logic error
- Compile error
- Logic error (it compiles, but sets the value instead of comparing). Warnings often catch it.
This should print the average of three numbers, but it prints the wrong answer. Find two problems.
int a { 4 };int b { 5 };int c { 7 };std::cout << a + b + c / 3;- Missing brackets:
c / 3happens first. It should be(a + b + c) / 3. - Integer division throws away the decimal part. Use
3.0:(a + b + c) / 3.0gives5.333....
What does it mean to "step into" a line in a debugger?
If the line calls a function, the debugger goes inside that function and pauses on its first line, so you can follow it step by step.
Tip: press ← / → to move between lessons, / to search.