CodeBlanks Logo Icon CodeBlanks

Putting it together

  • C course
  • Intermediate
  • Lesson 69/116
  • 6 min

Combine the library modules with a menu.

Putting it together with a menu

In the final step you combine all modules under a menu. The menu_run(Library *lib, int choice) function acts on choice: 1 lists all books, 2 checks out, 3 searches.

A switch (choice) branches the flow. Each case calls the relevant function. This structure is the skeleton of a real console app. In main we call the menu with fixed choices to test the output; in a real app you'd read the choice with scanf. In this final lesson all pieces come together: model, collection, checkout and search.

menu_run implementation

The void menu_run(Library *lib, int choice) function branches with switch. case 1: loop and print_book for all books. case 2: call checkout(&lib->books[0]). case 3: use find_by_title to search.

In main start with Library lib;, add a book, then call menu_run(&lib, 1);. This runs the menu's listing option. All modules meet here: print_book, library_add, checkout and find_by_title. The five-lesson project is complete; you have the skeleton of a real C console app.

Practice Integrate with menu_run

Add void menu_run(Library *lib, int choice) to the existing code. With switch (choice) add case 1: for (int i = 0; i < lib->count; i++) print_book(&lib->books[i]); and break;. In main add a book and call menu_run(&lib, 1);.

Starter code

main.c
#include <stdio.h>
#include <string.h>

typedef struct {
    char title[64];
    int year;
    int borrowed;
} Book;

typedef struct {
    char name[64];
    int id;
} Member;

typedef struct {
    Book books[10];
    int count;
} Library;

void library_init(Library *lib) {
    lib->count = 0;
}

void library_add(Library *lib, Book b) {
    lib->books[lib->count++] = b;
}

void print_book(Book *b) {
    printf("Book: %s (%d)\n", b->title, b->year);
}

int checkout(Book *b) {
    if (b->borrowed) return 0;
    b->borrowed = 1;
    return 1;
}

int return_book(Book *b) {
    if (!b->borrowed) return 0;
    b->borrowed = 0;
    return 1;
}

Book *find_by_title(Library *lib, const char *title) {
    for (int i = 0; i < lib->count; i++) {
        if (strcmp(lib->books[i].title, title) == 0) return &lib->books[i];
    }
    return NULL;
}

int main(void) {
    Library lib;
    library_init(&lib);
    Book b1 = {"C Tutorial", 2024, 0};
    library_add(&lib, b1);
    Book *found = find_by_title(&lib, "C Tutorial");
    if (found) print_book(found);
    return 0;
}

// Write menu_run function: switch with case 1 listing
// In main: call the menu with choice 1

Sample solution

main.c
#include <stdio.h>
#include <string.h>

typedef struct {
    char title[64];
    int year;
    int borrowed;
} Book;

typedef struct {
    char name[64];
    int id;
} Member;

typedef struct {
    Book books[10];
    int count;
} Library;

void library_init(Library *lib) {
    lib->count = 0;
}

void library_add(Library *lib, Book b) {
    lib->books[lib->count++] = b;
}

void print_book(Book *b) {
    printf("Book: %s (%d)\n", b->title, b->year);
}

int checkout(Book *b) {
    if (b->borrowed) return 0;
    b->borrowed = 1;
    return 1;
}

int return_book(Book *b) {
    if (!b->borrowed) return 0;
    b->borrowed = 0;
    return 1;
}

Book *find_by_title(Library *lib, const char *title) {
    for (int i = 0; i < lib->count; i++) {
        if (strcmp(lib->books[i].title, title) == 0) return &lib->books[i];
    }
    return NULL;
}

void menu_run(Library *lib, int choice) {
    switch (choice) {
        case 1:
            for (int i = 0; i < lib->count; i++) print_book(&lib->books[i]);
            break;
    }
}

int main(void) {
    Library lib;
    library_init(&lib);
    Book b1 = {"C Tutorial", 2024, 0};
    library_add(&lib, b1);
    menu_run(&lib, 1);
    return 0;
}

Expected output

Book: C Tutorial (2024)

Questions and answers

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

1 What does menu option 1 do?

  • Checks out
  • Exits
  • Searches
  • Lists books Correct answer

Explanation: Option 1 lists all books via print_book.

2 Complete the switch case

Question
switch (choice) {
case 1:
    for (int i = 0; i < lib->count; i++) ____(&lib->books[i]);
    break;
}
Answer
switch (choice) {
case 1:
    for (int i = 0; i < lib->count; i++) print_book(&lib->books[i]);
    break;
}

Explanation: case 1 prints each book with print_book.

3 Match menu options

Term Meaning
case 1 List books
case 2 Check out
case 3 Search
default Unknown choice

All lessons in Intermediate