🌿 Smart Garden Manager in C++ with Robotics, UI, Drones, and Sound "A Rainwater Conservation System for Tomorrow’s Farms"

Hey guys, today we are going to build a recipe book project. This is a console-based, simple recipe book where users can not only search and view but also add, update, or delete recipes in the system.
Each recipe will contain information such as the name, ingredients, and step-by-step method. All data will be stored in files for persistence. So what are we waiting for? Let's jump in and get started!
Used for saving the recipes and retrieving them for later use.
Used for storing and manipulating different recipes efficiently.
Used for managing code by dividing it into reusable parts.
Used for names of recipes, ingredients, and step-by-step instructions.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
// Structure to store recipe details
struct Recipe {
string name;
vector<string> ingredients;
string steps;
};
// Function to display a menu
void displayMenu() {
cout << "\n--- Recipe Book Menu ---\n";
cout << "1. Add Recipe\n";
cout << "2. View All Recipes\n";
cout << "3. Search Recipe by Name\n";
cout << "4. Search Recipe by Ingredient\n";
cout << "5. Update Recipe\n";
cout << "6. Delete Recipe\n";
cout << "7. Exit\n";
cout << "Choose an option: ";
}
// Function to add a new recipe
void addRecipe(vector<Recipe>& recipes, const string& filename) {
Recipe recipe;
cout << "Enter recipe name: ";
cin.ignore();
getline(cin, recipe.name);
cout << "Enter ingredients (comma-separated): ";
string ingredientsLine;
getline(cin, ingredientsLine);
stringstream ss(ingredientsLine);
string ingredient;
while (getline(ss, ingredient, ',')) {
recipe.ingredients.push_back(ingredient);
}
cout << "Enter steps: ";
getline(cin, recipe.steps);
recipes.push_back(recipe);
// Save to file
ofstream file(filename, ios::app);
if (file) {
file << recipe.name << "\n";
for (const auto& ing : recipe.ingredients) {
file << ing << ",";
}
file << "\n" << recipe.steps << "\n";
file << "-----\n";
file.close();
} else {
cout << "Error saving to file.\n";
}
}
int main() {
vector<Recipe> recipes;
const string filename = "recipes.txt";
int choice;
do {
displayMenu();
cin >> choice;
switch (choice) {
case 1:
addRecipe(recipes, filename);
break;
case 7:
cout << "Exiting program.\n";
break;
default:
cout << "Invalid choice. Try again.\n";
}
} while (choice != 7);
return 0;
}
All recipes are stored in a text file (recipes.txt
). Each recipe includes a name, ingredients, and a step-by-step process.
addRecipe
function.viewRecipes
function allows users to view all stored recipes.deleteRecipe
function allows users to delete a recipe and update storage.loadRecipes
function loads stored recipes from files, enabling persistent data handling.Restaurants often use digital recipe books to manage menu items efficiently and ensure consistency.
Users can save recipes based on dietary preferences (e.g., vegetarian, keto) and filter them by ingredients.
This project can be expanded into a web-based application where users share their special recipes with a larger community.
Implementing a robust search algorithm like fuzzy searching enables users to find recipes even with minor spelling errors.
Using a database instead of text files would allow for more scalable and efficient storage, especially for large applications.
I hope you enjoyed this project! Stay tuned for more cool C++ mini-projects.
Comments
Post a Comment