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

Image
  🌿  Smart Garden Manager in C++ with Robotics, UI, Drones, and Sound "A Rainwater Conservation System for Tomorrow’s Farms" 🧭  1. Introduction: Farming in the Age of Climate Change In a world where clean water is more precious than gold, efficient  rainwater harvesting and plant care systems  are no longer optional — they’re essential. Smart farming doesn’t mean just automating irrigation. It means combining  robotic drones, environmental sensors, and intelligent scheduling  to build a garden that practically takes care of itself. In this guide, we build a  fully functional Garden Manager System  using  C++  that: Captures and conserves rainwater Uses  robotic drones and sensors  to monitor crop health Integrates a  real-time UI  with progress bars and alerts Includes  timers  for scheduling plant growth and drone tasks Plays  interactive sounds  based on crop state and events Whether you'r...

C++ Projects: File Handling System

C++ Projects: File Handling System

Hello everyone! Today, we’ll explore a project that reads from and writes to text files, making it useful for applications like a diary or a logbook.

Features

This project allows users to:

  • Add new entries
  • View all entries
  • Search for keyword-based entries
  • Delete entries

Adding Entries

When a user writes an entry, it is automatically saved in a text file.

Viewing Entries

All entries present in the text file can be displayed.

Searching Entries

Users can find entries based on keywords.

Deleting Entries

Entries can be erased by selecting an entry number.

Skills Used

File Handling

  • ofstream: Writes data to files.
  • ifstream: Reads data from files.
  • ios::app: Opens a file in append mode to prevent overwriting.

Entry Management

Each entry is separated by ----- to maintain structure and readability.

Functions

  • addEntry: Saves user input into the file.
  • viewEntries: Reads and displays file content.
  • searchEntry: Searches for keywords within entries.
  • deleteEntry: Removes an entry by rewriting the file without it.

Menu System

A loop-based menu allows users to choose different actions until they decide to exit.


The Code

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

// Function to add a new entry
void addEntry() {
    ofstream outFile("diary.txt", ios::app); // Open file in append mode
    if (!outFile) {
        cout << "Error: could not open file.\n";
        return;
    }
    cout << "You can write your diary entry (type 'END' on a new line to finish):\n";
    string entry, line;
    while (getline(cin, line)) {
        if (line == "END") break;
        entry += line + "\n";
    }
    outFile << entry << "-----\n"; // Use "-----" to separate entries
    outFile.close();
    cout << "Entry saved successfully.\n";
}

// Function to view all entries
void viewEntries() {
    ifstream inFile("diary.txt");
    if (!inFile) {
        cout << "Error: could not open file.\n";
        return;
    }
    cout << "\nAll entries:\n====================\n";
    string line;
    while (getline(inFile, line)) {
        cout << line << endl;
    }
    inFile.close();
}

// Function to search for an entry
void searchEntry() {
    ifstream inFile("diary.txt");
    if (!inFile) {
        cout << "Error: could not open file.\n";
        return;
    }
    cout << "Enter keyword to search: ";
    string keyword;
    cin.ignore();
    getline(cin, keyword);
    string line, entry;
    bool found = false;
    while (getline(inFile, line)) {
        if (line == "-----") {
            if (entry.find(keyword) != string::npos) {
                cout << "\nFound Entry:\n====================\n" << entry << "====================\n";
                found = true;
            }
            entry.clear();
        } else {
            entry += line + "\n";
        }
    }
    if (!found) cout << "No entry found with the keyword " << keyword << "\n";
    inFile.close();
}

// Function to delete an entry
void deleteEntry() {
    ifstream inFile("diary.txt");
    if (!inFile) {
        cout << "Error: could not open file.\n";
        return;
    }
    vector<string> entries;
    string line, entry;
    while (getline(inFile, line)) {
        if (line == "-----") {
            entries.push_back(entry);
            entry.clear();
        } else {
            entry += line + "\n";
        }
    }
    inFile.close();

    cout << "\nEntries:\n";
    for (size_t i = 0; i < entries.size(); i++) {
        cout << i + 1 << ".\n" << entries[i] << "====================\n";
    }
    
    cout << "Enter the entry number to delete: ";
    int choice;
    cin >> choice;
    if (choice < 1 || choice > entries.size()) {
        cout << "Invalid choice.\n";
        return;
    }
    entries.erase(entries.begin() + choice - 1);
    ofstream outFile("diary.txt");
    for (const auto& e : entries) {
        outFile << e << "-----\n";
    }
    outFile.close();
    cout << "Entry deleted successfully.\n";
}

// Main Menu
int main() {
    int choice;
    do {
        cout << "\nEntry Management System\n";
        cout << "1. Add entry\n";
        cout << "2. View entries\n";
        cout << "3. Search entry\n";
        cout << "4. Delete entry\n";
        cout << "5. Exit\n";
        cout << "Enter your choice: ";
        cin >> choice;
        cin.ignore();
        switch (choice) {
            case 1: addEntry(); break;
            case 2: viewEntries(); break;
            case 3: searchEntry(); break;
            case 4: deleteEntry(); break;
            case 5: cout << "Exiting the program.\n"; break;
            default: cout << "Invalid choice. Please try again.\n";
        }
    } while (choice != 5);
    return 0;
}

Real-World Applications

Diary Writing

  • Users can record their thoughts daily.
  • Search functionality allows retrieving past experiences using keywords like “holiday” or “meeting.”

Employee Logbook

  • Tracks attendance and work hours efficiently.
  • Enhancements can include employee IDs and timestamps.

Bug Tracking System

  • Developers log issues with timestamps.
  • Entries can be categorized with tags like “UI Bug.”
  • Instead of deleting entries, marking them as “resolved” improves tracking.

Expense Tracker

  • Users can categorize expenses, e.g., “Food,” “Travel.”
  • Search functionality filters expenses by date or category.
  • Summarizing expenses over a period helps in financial planning.

This project is a great way to practice C++ file handling while building something useful. Hope you found this blog insightful—see you next time!

Comments

Popular posts from this blog

C++ Projects: Basic Traffic Management System

C++ Projects: Book Shop Management System

C++ Projects: Password Manager