English
Summary
Conclusions
Types define the allowed values, but the programmer checks that the domain data is correct. Initialization and limit checks prevent a large share of defects. Input requires checking both the stream state and the meaning of the value. Branches and loops build the algorithm, and the output format makes the result readable. Testing at the boundaries and with invalid tokens is needed even for a short console program.
Self-check questions
- How does a type differ from the current value of a variable?
- Why doesn’t
longin MSVC x64 necessarily take 8 bytes? - How do
min()andlowest()differ fordouble? - Which narrowing does brace initialization forbid?
- How do
const,constexpr, andautodiffer in purpose? - Why isn’t
chara universal type for a Unicode letter? - When is division an integer division, and how do you get a fractional result?
- Why should overflow be checked before the operation?
- What does
{:08.3f}mean, and does it limit the magnitude of the number? - Why are
clearandignoreneeded after an input error? - Why shouldn’t EOF be handled by endlessly repeating the prompt?
- When can the body of a
whileloop not execute even once? - How does
breakdiffer fromcontinue? - What is the difference between a random number engine and a distribution?
- Which tests are needed for a loop with user-defined limits and step?
Review questions for the lab
- How do syntactic and domain input validation differ?
- When are
int,long long, anddoubleappropriate? - What does an explicit conversion before division change?
- How do you check for overflow when adding nonnegative numbers?
- Why can’t you check a range with a chain of comparisons?
- How do you correctly recover a stream after an invalid token?
- How does
continueaffect the step of aforloop? - How does
switchdiffer fromiffor ranges? - What does the minimum width of a formatted field mean?
- How do you prove that a loop terminates for valid data?
Useful links
- MSVC types: https://learn.microsoft.com/cpp/cpp/fundamental-types-cpp.
- The MSVC formatting implementation: https://github.com/microsoft/STL/blob/main/stl/inc/format.
- Input: https://learn.microsoft.com/cpp/standard-library/basic-istream-class.
- Random numbers: https://learn.microsoft.com/cpp/standard-library/random.