Learn C · lesson 46 of 60 · about 10 minutes
Capstone: Core Logic
Write the bill's functions and a command loop that reads add, total and cat commands.
What you'll learn
- Write functions that take the bill by pointer
- Parse commands with sscanf
- Keep main small
The bill's jobs
Now the bill gets functions that take it by pointer. add_cost(&bill, what, category, pence) refuses a full bill or an amount that isn't above 0 and returns false, otherwise it copies the text in safely with snprintf and returns true. total_for(&bill, "compute") adds one category, and total_for(&bill, NULL) adds everything. Main reads commands until end: add web2 compute 900, cat compute, total, and anything else gets a list of the commands.
One function per job
add_expensechecks there's room, then stores the expensetotaladds every amounttotal_foradds only one category- main reads lines like
add Lunch food 650,totalorcat food
The core
#include <stdio.h>
#include <string.h>
typedef struct {
char what[24];
char category[12];
int pence;
} Expense;
typedef struct {
Expense items[50];
int count;
} Tracker;
int add_expense(Tracker *t, const char *what, const char *category, int pence) {
if (t->count == 50 || pence <= 0) return 0;
Expense *e = &t->items[t->count++];
snprintf(e->what, sizeof e->what, "%s", what);
snprintf(e->category, sizeof e->category, "%s", category);
e->pence = pence;
return 1;
}
int total_for(const Tracker *t, const char *category) {
int sum = 0;
for (int i = 0; i < t->count; i++) {
if (category == NULL || strcmp(t->items[i].category, category) == 0) sum += t->items[i].pence;
}
return sum;
}
int main(void) {
Tracker t = {.count = 0};
char line[80], what[24], category[12];
int pence;
while (fgets(line, sizeof line, stdin)) {
if (sscanf(line, "add %23s %11s %d", what, category, &pence) == 3) {
printf(add_expense(&t, what, category, pence) ? "added\n" : "rejected\n");
} else if (sscanf(line, "cat %11s", category) == 1) {
printf("%s: %d\n", category, total_for(&t, category));
} else if (strncmp(line, "total", 5) == 0) {
printf("total: %d\n", total_for(&t, NULL));
}
}
return 0;
}Output
added
added
added
rejected
food: 770
total: 975Tip
Passing NULL as the category to mean "everything" lets one function do both jobs. Document that kind of choice in a comment.
Pointers to the bill
add_cost takes Bill *b, because it has to change the real bill, not a copy of it. total_for only reads, so it takes const Bill *b, and the compiler stops it from changing anything by mistake. Passing a pointer also avoids copying the whole struct, all 8 costs, every time a function is called.
A copy changes nothing
#include <stdio.h>
typedef struct {
int count;
} Bill;
void add_copy(Bill b) {
b.count++;
}
void add_real(Bill *b) {
b->count++;
}
int main(void) {
Bill bill = {.count = 0};
add_copy(bill);
add_real(&bill);
printf("%d\n", bill.count);
return 0;
}Output
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.