Learn C · lesson 45 of 60 · about 8 minutes
Capstone: Planning and Modelling
Plan the server bill: list its features, design its structs, and store money safely as whole pence.
What you'll learn
- Turn an idea into features
- Design structs for the data
- Store money as integers
The server bill
The system report's capstone is the server bill: what the machines cost each month. Every cost is a Cost struct with what it's for, a category such as compute and the amount in whole pence, so £6.50 is 650. A Bill holds up to MAX_COSTS of them and counts how many it has. The admin types the standing costs one per line, like web1 compute 650, until end; each one is read with fgets, pulled apart with sscanf and printed with print_money, and a line that isn't a cost is reported instead of stored.
The program
Your capstone is an expense tracker that runs in the terminal:
1. Add an expense with a description, category and amount
2. Show the total, and the total for one category
3. List expenses sorted by amount
4. Save to a file and load it back
Before writing any code, decide what data you need.
Money is an int
Doubles can't store most decimals exactly: 0.1 + 0.2 isn't quite 0.3. For money that's unacceptable, so store pence in an int: £6.50 is 650.
To print it, split it: pence / 100 pounds and pence % 100 pence, with %02d so 5 pence shows as 05.
The model
#include <stdio.h>
typedef struct {
char what[24];
char category[12];
int pence;
} Expense;
typedef struct {
Expense items[50];
int count;
} Tracker;
void print_money(int pence) {
printf("£%d.%02d", pence / 100, pence % 100);
}
int main(void) {
Tracker t = {.count = 0};
t.items[t.count++] = (Expense){"Lunch", "food", 650};
t.items[t.count++] = (Expense){"Bus", "travel", 205};
for (int i = 0; i < t.count; i++) {
printf("%-8s %-8s ", t.items[i].what, t.items[i].category);
print_money(t.items[i].pence);
printf("\n");
}
return 0;
}Output
Lunch food £6.50
Bus travel £2.05Tip
(Expense){...} is a compound literal: a struct value written inline, handy for filling an array slot in one line.
Reading a cost from a line
sscanf(line, "%23s %11s %d", ...) reads three things from the line and gives back how many it managed, so anything other than 3 means the line wasn't a cost. The widths 23 and 11 are one less than the arrays, leaving room for the '\0'. Checking count == MAX_COSTS before storing means a full bill refuses politely instead of writing past the end of the array.
One line, three parts
#include <stdio.h>
int main(void) {
char what[24], category[12];
int pence = 0;
int got = sscanf("web1 compute 650", "%23s %11s %d", what, category, &pence);
printf("%d parts: %s, %s, %d pence\n", got, what, category, pence);
printf("%d\n", sscanf("oops", "%23s %11s %d", what, category, &pence));
return 0;
}Output
3 parts: web1, compute, 650 pence
1Now try it yourself
This lesson is free in Codiqo, with 9 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.