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
, andinstructions
. - 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 toMAX_RECIPES
recipes (defined as 5 in this case).
How to Run the Program
To run this program, follow these steps:
- Ensure that you have a C compiler installed (e.g., GCC or Turbo C).
- Copy and paste the provided C code into a text file and save it with a
.c
extension (e.g.,recipe_book.c
). - Open the terminal (or command prompt) and navigate to the directory where the file is saved.
- Compile the program using the following command:
gcc recipe_book.c -o recipe_book
- Run the compiled program:
./recipe_book
- Follow the on-screen menu to add and view recipes.