Explore how to create your very own digital recipe book using C++! This program allows you to store and display recipes efficiently.
Introduction
In today’s digital age, managing and storing recipes has never been easier. With this recipe book program, you can efficiently store your favorite recipes and display them when needed. This program allows the user to input the recipe details like title, ingredients, and steps, and later retrieve them as needed.
The objective of this C++ program is to help you build a simple digital recipe book. You will be able to add new recipes, view stored recipes, and manage them in an organized way.
Code: Digital Recipe Book Program in C++
#include #include #include using namespace std; class Recipe { public: string title; string ingredients; string steps; // Constructor to initialize the recipe Recipe(string t, string i, string s) : title(t), ingredients(i), steps(s) {} // Function to display recipe details void displayRecipe() { cout << "Recipe: " << title << endl; cout << "Ingredients: " << ingredients << endl; cout << "Steps: " << steps << endl; } }; int main() { vector recipeBook; int choice; do { cout << "\nDigital Recipe Book Menu:\n"; cout << "1. Add a new recipe\n"; cout << "2. View all recipes\n"; cout << "3. Exit\n"; cout << "Enter your choice: "; cin >> choice; cin.ignore(); // To ignore the newline character after entering choice if (choice == 1) { string title, ingredients, steps; cout << "Enter recipe title: "; getline(cin, title); cout << "Enter ingredients: "; getline(cin, ingredients); cout << "Enter steps: "; getline(cin, steps); Recipe newRecipe(title, ingredients, steps); recipeBook.push_back(newRecipe); cout << "Recipe added successfully!\n"; } else if (choice == 2) { if (recipeBook.empty()) { cout << "No recipes available.\n"; } else { for (int i = 0; i < recipeBook.size(); i++) { cout << "\nRecipe " << i + 1 << ":\n"; recipeBook[i].displayRecipe(); } } } } while (choice != 3); cout << "Exiting program. Goodbye!" << endl; return 0; }
Program Explanation
The program is a simple digital recipe book that allows the user to add, view, and manage recipes. Here’s a breakdown of the structure:
- Recipe Class: This class holds the title, ingredients, and steps for each recipe. It has a constructor to initialize these details and a method to display the recipe.
- Main Function: The main function uses a vector to store all recipes. The user is presented with a menu where they can add new recipes, view all recipes, or exit the program.
- Menu Loop: A do-while loop is used to continuously show the menu until the user chooses to exit. Inside the loop, the user can input a new recipe or view existing ones.
The program uses basic input/output and vector data structure from the C++ Standard Library to manage the list of recipes.
How to Run the Program
To run the program, follow these steps:
- Copy the C++ code provided above into a new file (e.g., recipe_book.cpp).
- Compile the program using a C++ compiler. If you’re using g++, you can run the following command in your terminal:
g++ recipe_book.cpp -o recipe_book
- Run the compiled program:
./recipe_book
- Follow the on-screen menu to add or view recipes.