You can now read a line and parse a number from it. A planner task line holds three fields of different kinds: a number, a flag and a title that may contain spaces. Parsing it means using the right tool for each field and checking the places between fields, because that is where text which looks fine to a person can be read in a way you did not intend.
Our planner input uses one line per task: minutes, a completion flag, then the title. For example, 25 0 Read chapter means an incomplete 25-minute task, and a flag of 1 means completed. Minutes must be 1 through 180, the flag must be exactly 0 or 1, and the title must contain 1 through 40 ASCII characters after leading whitespace is removed.
Predict and run
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::istringstream input("25 0 Read chapter");
int minutes = 0;
int done = 0;
std::string title;
if (input >> minutes >> done) {
std::getline(input >> std::ws, title);
std::cout << (done == 1 ? "Done: " : "Todo: ") << title << " " << minutes << "\n";
}
return 0;
}Check your prediction
Todo: Read chapter 25The two numbers came from >>, and the title came from std::getline, which kept the space inside Read chapter. input >> std::ws first skips the whitespace that separated the flag from the title, so the title does not start with a space. The expression condition ? first : second chooses one of two values; it is an expression, so it can sit inside an output chain. Change the flag to 1, then to 2. The 2 prints Todo:, which is wrong: nothing here validates the flag.
The gap between fields matters
>> skips whitespace before a number, but it does not require any. A sign can also start a number. Put those together and a line with no space at all can be read as two numbers:
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::istringstream input("25+0 Title");
int minutes = 0;
int done = 0;
if (input >> minutes >> done) {
std::cout << "minutes=" << minutes << " done=" << done << "\n";
}
return 0;
}minutes=25 done=0The first extraction read 25 and stopped at +. The second read +0 as the number zero. The line was accepted even though it does not match the format. A similar problem goes the other way: 25 0Title reads the flag 0 and then a title of Title, silently gluing two fields together. The fix is to read exactly one character after each number with input.get(separator) and require it to be a space or a tab.
Build the record, then commit it
Here is a parser for the whole format. It reads everything into a local Task candidate, checks each field and each gap, and assigns to out only when the record is completely valid.
#include <iostream>
#include <sstream>
#include <string>
struct Task {
std::string title;
int minutes = 0;
bool done = false;
};
bool is_gap(char c) {
return c == ' ' || c == '\t';
}
bool parse_task(const std::string& line, Task& out) {
std::istringstream input(line);
Task candidate;
int flag = 0;
char separator = '\0';
if (!(input >> candidate.minutes) || !input.get(separator) || !is_gap(separator)) {
return false;
}
if (!(input >> flag) || (flag != 0 && flag != 1)) {
return false;
}
if (!input.get(separator) || !is_gap(separator)) {
return false;
}
std::getline(input >> std::ws, candidate.title);
candidate.done = flag == 1;
if (candidate.minutes < 1 || candidate.minutes > 180 ||
candidate.title.empty() || candidate.title.size() > 40) {
return false;
}
out = candidate;
return true;
}
int main() {
Task task{"Keep", 1, false};
std::cout << parse_task("25 1 Review notes", task) << " " << task.title << "\n";
std::cout << parse_task("25+0 Title", task) << " " << task.title << "\n";
std::cout << parse_task("25 0Title", task) << " " << task.title << "\n";
std::cout << parse_task("25 0", task) << " " << task.title << "\n";
return 0;
}1 Review notes
0 Review notes
0 Review notes
0 Review notesAfter the one success, three different malformed lines were rejected and task kept the good record. 25 0 fails because no separator character follows the flag. The planner's real parse_task, which you will read on Day 7, adds two more checks: it rejects a title made only of whitespace and a line containing an embedded line break.
One more rule keeps the input loop healthy: use a new string stream for every line. A stream that failed on a bad line stays failed, so sharing one stream across lines would let a single bad line reject everything after it.
Practice
Your turn
Copy the Task struct and write bool valid_task(const Task& task) that checks the record rules on their own: minutes 1 through 180, and a title of 1 through 40 characters that contains at least one character other than a space or tab. Assert:
{"Read", 25, false}and a 40-character title with 180 minutes are valid- 0 minutes, 181 minutes, an empty title, a 41-character title and a title of three spaces are invalid
Build long titles with std::string(40, 'x'). When all pass, print Task checks passed.
#include <iostream>
#include <string>
#include <cassert>
struct Task {
std::string title;
int minutes = 0;
bool done = false;
};
int main() {
// Write valid_task above main and your assertions here.
return 0;
}Hint 1: the idea
Separate the record rules from the text format. The parser builds a Task; this function decides whether any Task, however it was built, is acceptable.
Hint 2: a first step
task.title.find_first_not_of(" \t") returns std::string::npos when the title has no character other than spaces and tabs. Combine that with the size and minutes checks using &&.
Show one solution
#include <iostream>
#include <string>
#include <cassert>
struct Task {
std::string title;
int minutes = 0;
bool done = false;
};
bool valid_task(const Task& task) {
return task.minutes >= 1 && task.minutes <= 180 &&
!task.title.empty() && task.title.size() <= 40 &&
task.title.find_first_not_of(" \t") != std::string::npos;
}
int main() {
assert(valid_task({"Read", 25, false}));
assert(valid_task({std::string(40, 'x'), 180, true}));
assert(!valid_task({"Zero", 0, false}));
assert(!valid_task({"Long", 181, false}));
assert(!valid_task({"", 25, false}));
assert(!valid_task({std::string(41, 'x'), 25, false}));
assert(!valid_task({" ", 25, false}));
std::cout << "Task checks passed\n";
return 0;
}Task checks passedEach assertion sits on one side of a boundary: 180 and 181, 40 and 41 characters, an empty title and one made only of spaces. Keeping the record rules in their own function means both the parser and the code that adds tasks can enforce them, which is exactly how the finished planner uses valid_task. In the checkpoint you will split these pieces into a header and source files.
Completion is saved in this browser only. It does not measure mastery.