Today you assemble the week's pieces into one program: a study report that reads one duration per line from standard input and prints a summary. Before any code, you need a brief: a precise statement of what the program must do for every input, including the awkward ones. A vague brief such as "add up the minutes" leaves the important decisions to whoever writes the code, and two people will decide differently. A precise brief can be turned into acceptance cases, inputs with their exact expected output, and those cases tell you when the work is done.
The brief
- Read one duration per input line until end-of-file.
- Accept 1 through 180 minutes, written as decimal digits only. Leading zeros are allowed, and the final line may lack its newline.
- Reject blank lines, spaces, signs, trailing text and out-of-range values.
- Keep at most 8 valid sessions. A valid line that arrives when the collection is full is rejected.
- A line too long for the 32-byte buffer is rejected once, however long it is, and reading continues with the next line.
- An invalid line never stops later lines from being read.
- Print three lines: the number of sessions, the total minutes and the number of rejected lines.
Data lives only during the run. The program saves no files and collects nothing about you.
Why these rules
Each rule closes a door that would otherwise let undefined behavior or a wrong report through. The range 1 through 180 means the largest possible total is 8 times 180, or 1440, far inside the range of an int, so the total cannot overflow. The fixed capacity of 8 means the program never needs to grow its storage and never writes past the array. Counting rejections, instead of stopping at the first bad line, means one typo does not throw away a whole day of data, and the rejected count tells the user something was skipped. The strict number format means the parser never has to guess what " 25" or "25min" meant.
Predict and run
This program only formats a report. Predict its three lines.
#include <stdio.h>
int main(void) {
int count = 3;
int total = 215;
int rejected = 2;
printf("Sessions: %d\n", count);
printf("Total: %d minutes\n", total);
printf("Rejected: %d\n", rejected);
return 0;
}Check your prediction
Sessions: 3
Total: 215 minutes
Rejected: 2These are the right numbers for one particular input, typed in by hand. They would be wrong for any other input, which is exactly why an example output is not an implementation.
Acceptance cases
Work out each expected report from the brief before you read any implementation. Each case targets a rule that a plausible mistake would break.
- Empty input gives 0 sessions, 0 minutes, 0 rejected. Nothing to read must still print a report.
- 25, 10, bad, 180, 0 gives 3, 215, 2. Words and zero are rejected; 180 is accepted at the boundary.
- Nine lines of 1 gives 8, 8, 1. The ninth valid line meets a full collection.
- Eight lines of 180 gives 8, 1440, 0. The largest possible total.
- A blank line, -1, 181 and 25x gives 0, 0, 4. Four different ways to be invalid.
- 025 gives 1, 25, 0. Leading zeros are allowed.
- 25 with no final newline gives 1, 25, 0. The last line still counts.
- 40 nines, then 25 gives 1, 25, 1. An overlong line is rejected once and the next line is still read.
- 40 zeros, then 10 gives 1, 10, 1. Overlong is rejected by length, even though its value would be 0.
A course script runs all nine of these against the real program, so the brief, the cases and the code cannot drift apart.
Practice
Your turn
Write void print_report(int count, int total, int rejected) that prints the three report lines and does nothing else. Call it for the empty case and then for nine lines of 1:
Sessions: 0
Total: 0 minutes
Rejected: 0
Sessions: 8
Total: 8 minutes
Rejected: 1#include <stdio.h>
int main(void) {
// Write your solution here.
return 0;
}Hint 1: the idea
A formatting function accepts data rather than inventing it. Every number it prints comes from a parameter, so the same function serves every case.
Hint 2: a first step
Use three int parameters, one for each printed field, and move the three printf lines from the example into the function body.
Show one solution
#include <stdio.h>
void print_report(int count, int total, int rejected) {
printf("Sessions: %d\n", count);
printf("Total: %d minutes\n", total);
printf("Rejected: %d\n", rejected);
}
int main(void) {
print_report(0, 0, 0);
print_report(8, 8, 1);
return 0;
}Sessions: 0
Total: 0 minutes
Rejected: 0
Sessions: 8
Total: 8 minutes
Rejected: 1The function owns the format and nothing else, so a change to the wording happens in one place and cannot disturb any calculation. The numbers you passed here were worked out from the brief; in the next lesson the real program calculates them from input, and the same expected values become the check.
Completion is saved in this browser only. It does not measure mastery.