CodeBlanks Logo Icon CodeBlanks

Adding products

Write the function that adds products to the cart.

The cart_add function

In this lesson we'll write the cart_add function. It adds a new item to the cart and increments cart_size.

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++;
}

strcpy copies the string; cart_size points to the next free slot. If the cart is full, we return early. This safety check prevents overflow.

Copying strings with strcpy

In C you can't assign strings with =; you must use strcpy. strcpy copies a string into a destination array character by character.

char name[32];
strcpy(name, "Apple");  // name is now "Apple"

That's why we use strcpy(cart[cart_size].name, name) inside cart_add. It's declared in the string.h header. To prevent overflow, strncpy can be preferred.

Practice Add two items

Write the cart_add function: void cart_add(const char *name, double price, int qty). If the cart is full (cart_size >= MAX_ITEMS) return early. Copy the name with strcpy, set price and qty, then cart_size++. main already adds two items; just complete the function. Expected output: 2 items added.

Starter code

main.c
#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;

// Write the cart_add function here

int main(void) {
    cart_add("Apple", 2.50, 3);
    cart_add("Bread", 5.00, 1);
    printf("%d items added\n", cart_size);
    return 0;
}

Sample solution

main.c
#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++;
}

int main(void) {
    cart_add("Apple", 2.50, 3);
    cart_add("Bread", 5.00, 1);
    printf("%d items added\n", cart_size);
    return 0;
}

Expected output

2 items added

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Increment cart_size

Question
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;
    ____;
}
Answer
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++;
}

Explanation: cart_size++ moves to the next free slot.

2 What's cart_size after cart_add?

main.c
cart_add("Apple", 2.50, 3);
cart_add("Bread", 5.00, 1);
  • 0
  • 2 Correct answer
  • 1
  • MAX_ITEMS

Explanation: After two adds cart_size = 2.

3 Match the add pieces

Term Meaning
strcpy Copies a string
cart_size++ Increments count
MAX_ITEMS Upper bound
void cart_add Function signature

All lessons in Beginner