C Tutorial
C Project: Inventory
Track item names, stock, and price. Print a table and the total value of the stock.
What you will build
An inventory is a list of items. Each item has a name, a quantity on the shelf, and a unit price. Line value is quantity times price. The total value of the stock is the sum of those line values. Print a table, then print that total.
Store a few records in an array of structs. Hardcode the demo so gcc can run with no typed input. ClickTry it in C for /c/try. That is the C editor.
Name, quantity, price
Three fields, three types: a char array, an int, and a double. Print one row to prove the layout before you add a loop.
Example
#include <stdio.h>
struct Item {
char name[24];
int qty;
double price;
};
int main(void) {
struct Item nail = {"nails", 100, 0.05};
double line = nail.qty * nail.price;
printf("%s: %d x %.2f = %.2f\n", nail.name, nail.qty, nail.price, line);
return 0;
}100 nails at 0.05 should print 5.00. If you see 0.00, the price field never received 0.05, or you multiplied with integers only.
A table of stock
Put several items in an array. Print a header, then one row per item. Align columns with a width in the format string so names, counts, and money line up.
Example
#include <stdio.h>
struct Item {
char name[24];
int qty;
double price;
};
int main(void) {
struct Item stock[] = {
{"nails", 100, 0.05},
{"screws", 40, 0.12},
{"hinges", 8, 2.50},
{"paint", 3, 14.99},
};
int n = (int)(sizeof stock / sizeof stock[0]);
printf("%-10s %6s %8s %10s\n", "item", "qty", "price", "value");
for (int i = 0; i < n; i++) {
double value = stock[i].qty * stock[i].price;
printf("%-10s %6d %8.2f %10.2f\n", stock[i].name, stock[i].qty,
stock[i].price, value);
}
return 0;
}Compile the table at /c/try. Check that hinges show 20.00 in the value column. Use the C gcc editor, not the Python, HTML, or C++ playground.
Total value of the stock
Add each line value into a running total. Print the total after the table. That number is the point of the project: how much money sits on the shelf if you value every unit at its listed price.
Example
#include <stdio.h>
struct Item {
char name[24];
int qty;
double price;
};
double line_value(const struct Item *it) {
return it->qty * it->price;
}
int main(void) {
struct Item stock[] = {
{"nails", 100, 0.05},
{"screws", 40, 0.12},
{"hinges", 8, 2.50},
{"paint", 3, 14.99},
};
int n = (int)(sizeof stock / sizeof stock[0]);
double total = 0.0;
printf("%-10s %6s %8s %10s\n", "item", "qty", "price", "value");
for (int i = 0; i < n; i++) {
double value = line_value(&stock[i]);
total += value;
printf("%-10s %6d %8.2f %10.2f\n", stock[i].name, stock[i].qty,
stock[i].price, value);
}
printf("%-10s %6s %8s %10.2f\n", "total", "", "", total);
return 0;
}5.00 + 4.80 + 20.00 + 44.97 is 74.77. If your total differs, print each line value on its own first. Do not round in the middle of the sum; let printf round only for display.
Find the dearest line
After the total works, walk the array once more to find the item whose line value is largest. That is the shelf line you would miss most if it vanished. Reuse line_value so the multiply lives in one place.
Example
#include <stdio.h>
struct Item {
char name[24];
int qty;
double price;
};
double line_value(const struct Item *it) {
return it->qty * it->price;
}
int main(void) {
struct Item stock[] = {
{"nails", 100, 0.05},
{"screws", 40, 0.12},
{"hinges", 8, 2.50},
{"paint", 3, 14.99},
};
int n = (int)(sizeof stock / sizeof stock[0]);
int richest = 0;
double total = 0.0;
for (int i = 0; i < n; i++) {
double value = line_value(&stock[i]);
total += value;
if (value > line_value(&stock[richest])) {
richest = i;
}
}
printf("stock value: %.2f\n", total);
printf("largest line: %s (%.2f)\n", stock[richest].name,
line_value(&stock[richest]));
return 0;
}Paint should win: 44.97 against 20.00 for hinges. Quantity 3 is small, but the unit price is not.
Common mistakes
- Storing price as
int. Then 0.05 becomes 0 and the nails line is worthless. - Printing money with
%d. Use%.2ffor two decimal places. - Multiplying in the total and again in the table with two different expressions that drift apart. One helper keeps them equal.
- Forgetting that
qtyis a count. A restock should add toqty, not replace the name.
Practice
- Print a second total that counts units: the sum of every
qty. - Mark any item with
qtybelow 5 as low stock in an extra column. - Add a restock of 10 nails in
mainand print the table before and after so the nails value changes from 5.00 to 5.50.