Every part of the report already exists: bounded reading from Day 6, the parser, the capacity-checked add_session, and the total from Day 4. Assembling them is mostly a matter of deciding who does what. The project splits three jobs across three files. main.c reads lines and counts rejections. report.c validates text and stores accepted sessions. report.h states the contracts between them. Because each helper keeps its own promises, main can be short: it never indexes the array itself, never converts a digit and never checks the range.
Predict and run
This small loop has the same shape as the report's main loop. Predict both numbers.
#include <stdio.h>
int main(void) {
int accepted = 0;
int rejected = 0;
int values[3] = {25, 0, 10};
for (int i = 0; i < 3; i++) {
if (values[i] < 1) {
rejected++;
continue;
}
accepted++;
}
printf("%d %d\n", accepted, rejected);
return 0;
}Check your prediction
2 1continue ends the current iteration at once and goes on to the next one, so a rejected value never reaches accepted++. Move accepted++ above the if and the result becomes 3 and 1: the bad value was counted on both sides. Each record must land in exactly one count.
Trace one line through the files
Follow the line 25 and then the line bad through the program in the editor at the end of this lesson.
fgets(line, sizeof line, stdin)reads into a 32-byte buffer."25\n"fits, with room to spare.- The overlong check,
strchr(line, '\n') == NULL && !feof(stdin), is false because the newline is present. parse_minutes(line, &minutes)accepts the digits and the newline and stores 25. For"bad"it returns 0 andminuteskeeps its starting 0.add_session(sessions, &count, minutes)checks the capacity and range, stores the session and incrementscount.- The two calls are joined in one condition:
!parse_minutes(...) || !add_session(...). If parsing fails, short-circuiting meansadd_sessionis never called, so an unparsed value can never be stored. If either step fails,rejected++runs once.
That one || is the whole policy: a line counts as a session only if it passes both checks, and every other line counts as one rejection.
The overlong line, step by step
A line of 40 digits does not fit in 32 bytes. fgets returns the first 31 characters with no newline. The program does not try to parse that fragment. It reads and discards characters with getchar until it reaches the newline or EOF, counts one rejection, and uses continue to start the next line cleanly. Without the drain, the rest of the line would arrive as a second fragment and might even parse as a valid number.
Two boundaries are worth tracing. A final line with no newline, such as 25 at the very end of the input, also lacks '\n', but feof(stdin) is true, so it is parsed normally. And a line of exactly 30 characters plus its newline fills 31 bytes and fits; one more character makes it overlong.
Errors are not end-of-file
fgets returns NULL both at the end of the input and when reading fails. After the loop, ferror(stdin) tells the two apart. On a real read error, the program writes a message to stderr, the error stream, and returns 1 from main. A nonzero exit status tells scripts and other programs that the run failed, while the normal report on stdout is kept for successful runs.
Practice
Your turn
Simulate the report's policy over an array. For {25, 0, 10, 181, 30, 20}, store valid durations (1 through 180) in a three-slot array. A value is rejected if it is invalid or if the array is already full. Use one if with ||, so that a full array is only checked for valid values, and print:
Stored 3, total 65, rejected 3#include <stdio.h>
int main(void) {
int input[6] = {25, 0, 10, 181, 30, 20};
int stored[3] = {0};
int count = 0;
// Write your solution here.
return 0;
}Hint 1: the idea
Write a small store function that returns 0 when the collection is full and 1 after storing. Then the loop body mirrors main.c: if the value is invalid, or storing it fails, count a rejection.
Hint 2: a first step
Write valid and store first, and test store on its own: call it four times on the empty three-slot array from main and print each result. You should see 1, 1, 1 and then 0. Only then write the loop over input.
Show one solution
#include <stdio.h>
int valid(int minutes) {
return minutes >= 1 && minutes <= 180;
}
int store(int values[], int *count, int value) {
if (*count >= 3) {
return 0;
}
values[*count] = value;
(*count)++;
return 1;
}
int main(void) {
int input[6] = {25, 0, 10, 181, 30, 20};
int stored[3] = {0};
int count = 0;
int rejected = 0;
for (int i = 0; i < 6; i++) {
if (!valid(input[i]) || !store(stored, &count, input[i])) {
rejected++;
}
}
int total = 0;
for (int i = 0; i < count; i++) {
total += stored[i];
}
printf("Stored %d, total %d, rejected %d\n", count, total, rejected);
return 0;
}Stored 3, total 65, rejected 30 and 181 were rejected by valid, and 20 by store, because the array was full after 30. Each value produced exactly one outcome, so stored plus rejected equals the six inputs, a check you can apply to any run of the report. The total loop uses count, never the capacity. Next you will test the real helpers at their boundaries.
Run the complete program
The input box is prefilled with 25, 10, bad, 180 and 0 on separate lines.
Check your prediction
Sessions: 3
Total: 215 minutes
Rejected: 2Now run the acceptance cases from the brief. Empty the input box, then try nine lines containing 1. Finally insert a line of 40 digits followed by a valid 25: the long line should be rejected once and the valid line still accepted.
Completion is saved in this browser only. It does not measure mastery.