Your guide to creating a digital recipe book using C language.

Introduction

Creating a digital recipe book is a fun and practical way to learn programming. In this tutorial, we will use the C programming language to develop a simple recipe management system. By the end of this project, you will be able to store, display, and manage recipes digitally, all while improving your programming skills.

Objective

The main objective of this project is to provide a basic structure for creating a recipe book using C programming. The system will allow users to enter, store, and display recipes, with each recipe containing a title, ingredients, and instructions. We will also implement simple navigation features to enhance the user experience.

The Program Code

#include 
#include 
#include 

// Define the maximum number of recipes
#define MAX_RECIPES 5

// Structure to represent a Recipe
struct Recipe {
    char title[100];
    char ingredients[300];
    char instructions[500];
};

// Function to display a recipe
void displayRecipe(struct Recipe r) {
    printf("\nTitle: %s\n", r.title);
    printf("Ingredients: %s\n", r.ingredients);
    printf("Instructions: %s\n", r.instructions);
}

// Function to input a recipe
void inputRecipe(struct Recipe *r) {
    printf("Enter the recipe title: ");
    fgets(r->title, sizeof(r->title), stdin);
    r->title[strcspn(r->title, "\n")] = '\0'; // Remove the newline character

    printf("Enter the ingredients: ");
    fgets(r->ingredients, sizeof(r->ingredients), stdin);
    r->ingredients[strcspn(r->ingredients, "\n")] = '\0';

    printf("Enter the instructions: ");
    fgets(r->instructions, sizeof(r->instructions), stdin);
    r->instructions[strcspn(r->instructions, "\n")] = '\0';
}

int main() {
    struct Recipe recipes[MAX_RECIPES];
    int recipeCount = 0;
    int choice;

    // Menu-driven program
    do {
        printf("\nRecipe Book Menu:\n");
        printf("1. Add a Recipe\n");
        printf("2. View Recipes\n");
        printf("3. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        getchar(); // To consume the newline left by scanf

        switch(choice) {
            case 1:
                if (recipeCount < MAX_RECIPES) {
                    inputRecipe(&recipes[recipeCount]);
                    recipeCount++;
                    printf("Recipe added successfully!\n");
                } else {
                    printf("Recipe book is full! You can't add more recipes.\n");
                }
                break;
            case 2:
                if (recipeCount == 0) {
                    printf("No recipes available to display.\n");
                } else {
                    printf("\nDisplaying Recipes:\n");
                    for (int i = 0; i < recipeCount; i++) {
                        displayRecipe(recipes[i]);
                    }
                }
                break;
            case 3:
                printf("Exiting the program. Goodbye!\n");
                break;
            default:
                printf("Invalid choice. Please try again.\n");
        }
    } while (choice != 3);

    return 0;
}

Explanation of the Program

This C program creates a digital recipe book where users can add, view, and manage recipes. Below is a breakdown of how the program works:

  • Structures: We use a structure called Recipe to store information about each recipe. The structure contains three fields: title, ingredients, and instructions.
  • Functions: There are two main functions:
    • inputRecipe: This function allows users to enter the title, ingredients, and instructions for a recipe.
    • displayRecipe: This function displays the details of a recipe in a readable format.
  • Menu: The program runs a menu-driven loop that allows users to choose between adding a recipe, viewing recipes, or exiting the program.
  • Array of Recipes: We maintain an array of Recipe structures to store up to MAX_RECIPES recipes (defined as 5 in this case).

How to Run the Program

To run this program, follow these steps:

  1. Ensure that you have a C compiler installed (e.g., GCC or Turbo C).
  2. Copy and paste the provided C code into a text file and save it with a .c extension (e.g., recipe_book.c).
  3. Open the terminal (or command prompt) and navigate to the directory where the file is saved.
  4. Compile the program using the following command:
    gcc recipe_book.c -o recipe_book
  5. Run the compiled program:
    ./recipe_book
  6. Follow the on-screen menu to add and view recipes.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)