Putting it together
- C course
- Beginner
- Lesson 52/116
- 6 min
Combine the cart functions with a menu.
Put the cart together
In the final lesson we combine all functions: add items with cart_add, list them with cart_list, find the subtotal with cart_subtotal and print the grand total.
int main(void) {
cart_add("Apple", 2.50, 3);
cart_add("Bread", 5.00, 1);
cart_list();
double sub = cart_subtotal();
double total = sub + sub * 0.18;
printf("Subtotal: %.2f\n", sub);
printf("Total: %.2f\n", total);
return 0;
}This small app is the foundation of a real shopping cart: data structure, adding, listing and total. It serves as a base for larger apps.
Project recap
In this project you built a shopping cart:
- Data model with the CartItem struct
- Item adding with cart_add
- Subtotal computation with cart_subtotal
- Formatted listing with cart_list
- Putting it all together in main
In a real app you'd also add a menu loop, file saving or user input. But the base structure stays the same: data structure + functions + main flow. Large C programs grow from such small pieces.
Practice Combine the cart
Write the main function. Include cart_add("Apple", 2.50, 3); and cart_add("Bread", 5.00, 1); calls, a cart_list(); call, double sub = cart_subtotal();, double total = sub + sub * 0.18; and printf("Subtotal: %.2f\n", sub); and printf("Total: %.2f\n", total);. The other functions are already defined. Expected output: Apple 2.50 x 3 = 7.50 / Bread 5.00 x 1 = 5.00 / Subtotal: 12.50 / Total: 14.75.
Starter code
#include <stdio.h>
#include <string.h>
#define MAX_ITEMS 10
struct CartItem {
char name[32];
double price;
int qty;
};
struct CartItem cart[MAX_ITEMS];
int cart_size = 0;
void cart_add(const char *name, double price, int qty) {
if (cart_size >= MAX_ITEMS) return;
strcpy(cart[cart_size].name, name);
cart[cart_size].price = price;
cart[cart_size].qty = qty;
cart_size++;
}
double cart_subtotal(void) {
double sum = 0;
for (int i = 0; i < cart_size; i++) {
sum += cart[i].price * cart[i].qty;
}
return sum;
}
void cart_list(void) {
for (int i = 0; i < cart_size; i++) {
printf("%s %.2f x %d = %.2f\n", cart[i].name, cart[i].price, cart[i].qty, cart[i].price * cart[i].qty);
}
}
int main(void) {
// Add two items, list them, print subtotal and total
return 0;
}Sample solution
#include <stdio.h>
#include <string.h>
#define MAX_ITEMS 10
struct CartItem {
char name[32];
double price;
int qty;
};
struct CartItem cart[MAX_ITEMS];
int cart_size = 0;
void cart_add(const char *name, double price, int qty) {
if (cart_size >= MAX_ITEMS) return;
strcpy(cart[cart_size].name, name);
cart[cart_size].price = price;
cart[cart_size].qty = qty;
cart_size++;
}
double cart_subtotal(void) {
double sum = 0;
for (int i = 0; i < cart_size; i++) {
sum += cart[i].price * cart[i].qty;
}
return sum;
}
void cart_list(void) {
for (int i = 0; i < cart_size; i++) {
printf("%s %.2f x %d = %.2f\n", cart[i].name, cart[i].price, cart[i].qty, cart[i].price * cart[i].qty);
}
}
int main(void) {
cart_add("Apple", 2.50, 3);
cart_add("Bread", 5.00, 1);
cart_list();
double sub = cart_subtotal();
double total = sub + sub * 0.18;
printf("Subtotal: %.2f\n", sub);
printf("Total: %.2f\n", total);
return 0;
}Expected output
Apple 2.50 x 3 = 7.50 Bread 5.00 x 1 = 5.00 Subtotal: 12.50 Total: 14.75
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 How is the grand total computed?
double sub = cart_subtotal();
double total = sub + sub * 0.18;- Subtotal + tax Correct answer
- Only the subtotal
- Only the tax
- Quantity × price
Explanation: total = sub + sub * 0.18.
2 Call the list
cart_add("Apple", 2.50, 3);
cart_add("Bread", 5.00, 1);
____();
cart_add("Apple", 2.50, 3);
cart_add("Bread", 5.00, 1);
cart_list();
Explanation: cart_list() prints the cart items.
3 Match the project steps
| Term | Meaning |
|---|---|
cart_add |
Adds items |
cart_list |
Lists |
cart_subtotal |
Subtotal |
main |
Combines |