You have every piece of the planner: a parser that validates a line, a function that adds a task within the limit, and a summary that reads the stored tasks. Assembling them is where small decisions matter most. Where does a rejected line get counted? What happens to the stored tasks when line 57 is bad? When is it safe to print? This lesson walks through main.cpp and explains why each step is where it is.
The flow has three stages. Read: main.cpp takes one line at a time from std::cin. Accept or reject: parse_task validates the whole record before changing its output, and add_task checks the record and the 100-task limit before copying it into the vector. Report: summarize reads the stored tasks through a const reference, and main prints the list and the totals.
Predict and run
#include <iostream>
#include <vector>
int main() {
std::vector<int> values{25, 0, 10};
int total = 0;
for (int value : values) {
if (value < 1 || value > 180) {
continue;
}
total += value;
}
std::cout << total << "\n";
return 0;
}Check your prediction
35continue skips the rest of the current iteration and moves on to the next value. The 0 is rejected without stopping the loop, so the 10 after it still counts. Move total += value; above the if and the 0 is added before it is checked: harmless here, but with 500 it would silently inflate the total. Validation must come before any effect.
The input loop, line by line
The heart of main.cpp is this loop:
while (std::getline(std::cin, line)) {
if (!line.empty() && line.back() == '\r') {
line.pop_back();
}
Task task;
if (!parse_task(line, task) || !add_task(tasks, task)) {
++rejected;
}
}- Whole lines first. Each record is read with
std::getline, so a malformed line is consumed completely and cannot confuse the next read. - Carriage returns. Text saved on Windows ends lines with
\r\n.getlineremoves the\nbut keeps the\r, which would otherwise end up in the title. The!line.empty()check comes first soline.back()is never called on an empty string, which would be undefined behavior. - A fresh
Taskevery iteration.taskis declared inside the loop body, so it is created and destroyed once per line. Nothing from a previous line can leak into the next one. - Short-circuit
||. If parsing fails,add_tasknever runs, so an invalid record can never be stored. If parsing succeeds but the planner is full,add_taskfails. Either way the line is counted once, as rejected.
Inside parse_task, a separator character must follow the minutes and the flag, which stops 25+0 from being read as two adjacent numbers and 0Title from being accepted as a flag and a title. A fresh string stream is created for each line, so a failed parse leaves no sticky failure behind.
After the loop
The loop ends when getline fails. Usually that is a normal end-of-file, but it can also be a real read error. std::cin.bad() distinguishes the two: it is true only for a serious failure, and then main writes a message to std::cerr and returns 1 instead of printing a report built from partial data.
The report is printed only after all input has been read, because the totals are not known until the end. The listing loop uses const Task&, and summarize takes const std::vector<Task>&, so the reporting stage cannot change a task.
Why the limit is 100
The 100-task limit bounds the report's arithmetic: at most 100 times 180, or 18,000 minutes, so no total can approach the largest int. It is a product rule, not a memory limit for every input line. getline can still allocate space for one very long rejected line, and the project's checks include a 1,000-character line to prove it is rejected without affecting the next one. The program stores nothing after a run ends.
Practice
Your turn
Simulate the accept-or-reject stage with plain durations. From the values 25, 0, 10, 181 and 30, store valid durations (1 through 180) in a vector, but never more than 2. Count everything else as rejected, including valid values that arrive when the vector is full. Print:
Accepted: 2Rejected: 3Stored: 25 10
Keep reading after every rejection.
#include <iostream>
#include <vector>
int main() {
std::vector<int> input{25, 0, 10, 181, 30};
const std::size_t limit = 2;
// Write your solution here.
return 0;
}Hint 1: the idea
Each value belongs to exactly one category. A value is stored only if it is valid and there is room; failing either check makes it rejected.
Hint 2: a first step
Write if (value < 1 || value > 180 || stored.size() >= limit) { ++rejected; continue; }, then push_back the value.
Show one solution
#include <iostream>
#include <vector>
int main() {
std::vector<int> input{25, 0, 10, 181, 30};
const std::size_t limit = 2;
std::vector<int> stored;
int rejected = 0;
for (int value : input) {
if (value < 1 || value > 180 || stored.size() >= limit) {
++rejected;
continue;
}
stored.push_back(value);
}
std::cout << "Accepted: " << stored.size() << "\n";
std::cout << "Rejected: " << rejected << "\n";
std::cout << "Stored:";
for (int value : stored) {
std::cout << " " << value;
}
std::cout << "\n";
return 0;
}Accepted: 2
Rejected: 3
Stored: 25 10The 30 is a valid duration, but it arrives when the vector already holds 2 values, so the capacity check rejects it, just as the planner rejects a valid 101st task. The accepted count comes from stored.size() rather than a separate counter, so the two can never disagree. Now run the real planner below and trace the same stages.
Run the complete planner
The input box holds the acceptance example from the brief.
Check your prediction
Todo: Read chapter (25 min)
Done: Review notes (10 min)
Todo: Practice (180 min)
Tasks: 3
Planned: 215 minutes
Completed: 10 minutes
Remaining: 205 minutes
Rejected: 1Now challenge it. Clear the input and run again: every total should be zero. Place a bad line before 25 0 Read and check that Read is still accepted. Try 25+0 Title, 0 0 Zero and a 41-character title, predicting the rejected count first. Change one completion flag to 1 and explain which totals change: planned stays the same, and completed plus remaining always equals planned.
Completion is saved in this browser only. It does not measure mastery.