Following a walkthrough shows that you can recognize good code. Building something new from a brief shows that you can produce it. This checkpoint asks for a whole program in a different domain, a reading list, so you cannot copy the planner line by line. You decide the records, the functions, where validation goes and what you test. There is no plan below on purpose: the brief, the sample and your own tests are all you get.
The brief
A reading planner reads one entry per line from standard input, in this format: a page count, a finished flag (0 or 1), then the title, separated by spaces. For example, 20 0 Dune is an unfinished 20-page book called Dune.
- Accept page counts of 1 through 500 and titles of 1 through 40 characters. Any other line, including one that is not in the format at all, is rejected and counted.
- Print each accepted entry, in input order, as
Reading: Title (N pages)orFinished: Title (N pages). - Then print the number of accepted lines, the number of rejected lines, and the total pages of accepted entries that are not finished.
- Keep parsing, validation and totals in functions, and only
mainreads input and prints. - Test those functions with assertions that run before the report: every boundary in the rules above, a line that is not a number, a bad flag, and empty input.
With this input:
20 0 Dune
10 1 Emma
0 0 Broken
500 0 Middlemarch
501 0 Too long
later 0 Notesthe program prints exactly:
Reading: Dune (20 pages)
Finished: Emma (10 pages)
Reading: Middlemarch (500 pages)
Accepted: 3
Rejected: 3
Unfinished pages: 520Predict and run
#include <iostream>
int main() {
int pages = 20;
bool finished = false;
std::cout << (finished ? 0 : pages) << "\n";
return 0;
}Check your prediction
20This line handles one record's contribution to unfinished pages, and nothing else. Before starting, list what the brief needs that it does not do. Then decide your function names and contracts, and write them as comments, before any code.
Practice
Your turn
Build the reading planner from the brief. The sample input is loaded into the editor, and your edits are saved in this browser. The starter only counts lines, so you can see the input arrive.
Hint 1: the idea
This is the study planner's shape in a new domain: one function turns a line into a record or reports failure without changing its output, one rule decides validity, and one function adds up a vector it only reads. Rejecting a line is a normal result, so it is counted, not printed as an error.
Hint 2: a first step
Write the record and the parsing function first, and test only it: "20 0 Dune" succeeds and fills the record, and "501 0 Too long" fails and leaves a record you filled beforehand unchanged. Leave the report for last.
Show one solution
One complete answer, with its checks in run_checks():
Validation lives in valid_reading, so parsing cannot accept a record the rules reject, and parse_reading only writes to out after every check has passed, which the 41-character case proves by reading the earlier Limit entry back. Each bound has a case on both sides: 1 and 500 pages pass while 0 and 501 fail, and a 1-character and a 40-character title pass while 41 characters fail. Reading lines into a vector first lets summarize be tested on empty input, which must give three zeros, as well as on a mix of good, bad and empty lines. The flag is read as an int and checked, so 20 2 Flag is rejected instead of quietly becoming true. unfinished_pages takes a const reference: it reads the plan and cannot change it, and the empty vector gives 0 through the loop's starting total with no special case. Your version can differ in names and layout. What matters is that each rule in the brief has an assertion that would fail if the rule broke.
Check your own work
Before you look at the solution, or after you finish, answer each of these honestly. Each "no" points at the lesson to revisit.
- Does your program print the sample output exactly, character for character? (Day 1, Day 4)
- Do 1 and 500 pages pass while 0 and 501 fail, and does each of those cases have its own assertion? (Day 3)
- Does a failed parse leave the record unchanged, and is there an assertion that shows it? (Day 6)
- Do your totals take the vector by
constreference? (Day 5) - Break one rule on purpose, such as
pages <= 500topages < 500. Does an assertion stop the run? If not, which case is missing? (Day 3) - Could someone tell from your function names and comments what each one promises, without reading the bodies? (Day 6)
Completion is saved in this browser only. It does not measure mastery.