Birthday Reminder – C++ Program

Birthday Reminder - C++ Program

The Birthday Reminder program is a simple console-based application written in C++. Its purpose is to help users keep track of important dates, such as birthdays, by allowing them to add, delete, and view reminders associated with individuals. The program stores reminder information, including the person’s name, birth date, and relationship, in a file named “Birthday.dat.”

Important Notes for Programmers

  • File Handling: The program uses file handling to store and retrieve reminder information. std::fstream is used for both input and output operations on the “Birthday.dat” file.
  • Structures: The program employs C++ structures (struct) to define the data structure for reminders. There are structures for the date (Date) and the reminder itself (Reminder).
  • Input Validation: Basic input validation is performed to ensure that the user provides valid numerical choices in the menu.
  • Function Usage: The program utilizes functions to modularize the code and improve readability. Functions like addReminder, deleteReminder, and viewReminder encapsulate specific functionalities.

Birthday Reminder Program

#include <iostream>
#include <fstream>
#include <cstring>

struct node {
    char ch;
    node* next;
};

struct Date {
    char dd[3];
    char mm[3];
    char yy[5];
};

struct Reminder {
    char name[50];
    Date datebd;
    char rel[50];
};

void enterLineText(const char* prompt, char* str, int maxLength) {
    std::cout << prompt;
    std::cin.getline(str, maxLength);
}

void addReminder(std::fstream& file) {
    Reminder rem;
    enterLineText("Enter Name: ", rem.name, sizeof(rem.name));
    enterLineText("Enter Birth Date (DD MM YYYY): ", rem.datebd.dd, sizeof(rem.datebd.dd));
    enterLineText("Enter Relationship: ", rem.rel, sizeof(rem.rel));

    file.write(reinterpret_cast<char*>(&rem), sizeof(rem));
    std::cout << "Reminder successfully Added!\n";
}

void deleteReminder(std::fstream& file) {
    char name[50];
    bool found = false;

    enterLineText("Enter Name to Delete: ", name, sizeof(name));

    std::fstream tempFile("Temp.dat", std::ios::out | std::ios::binary);
    Reminder rem;

    while (file.read(reinterpret_cast<char*>(&rem), sizeof(rem))) {
        if (strcmp(name, rem.name) != 0) {
            tempFile.write(reinterpret_cast<char*>(&rem), sizeof(rem));
        }
        else {
            found = true;
        }
    }

    tempFile.close();
    file.close();

    remove("Birthday.dat");
    rename("Temp.dat", "Birthday.dat");

    if (found) {
        std::cout << "Reminder successfully Deleted!\n";
    }
    else {
        std::cout << "Reminder Not Found!\n";
    }
}

void viewReminder(std::fstream& file) {
    char name[50];
    bool found = false;

    enterLineText("Enter Name to View: ", name, sizeof(name));

    file.seekg(0, std::ios::beg);
    Reminder rem;

    while (file.read(reinterpret_cast<char*>(&rem), sizeof(rem))) {
        if (strcmp(name, rem.name) == 0) {
            std::cout << "Name: " << rem.name << "\n";
            std::cout << "Birth Date: " << rem.datebd.dd << "/" << rem.datebd.mm << "/" << rem.datebd.yy << "\n";
            std::cout << "Relationship: " << rem.rel << "\n";
            found = true;
            break; // assuming only one entry per name
        }
    }

    if (!found) {
        std::cout << "Reminder not found!\n";
    }
}

int main() {
    std::fstream bdFile("Birthday.dat", std::ios::in | std::ios::out | std::ios::binary | std::ios::app);

    while (true) {
        std::cout << "\n1. Add Reminder\n";
        std::cout << "2. Delete Reminder\n";
        std::cout << "3. View Reminder\n";
        std::cout << "4. Exit\n";
        std::cout << "Enter your choice: ";

        int choice;
        std::cin >> choice;

        if (std::cin.fail()) {
            // Handle invalid input (non-integer)
            std::cin.clear(); // Clear the error flag
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Discard invalid input
            std::cout << "Invalid input. Please enter a number.\n";
            continue; // Restart the loop
        }

        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Clear the input buffer

        switch (choice) {
        case 1:
            addReminder(bdFile);
            break;
        case 2:
            deleteReminder(bdFile);
            break;
        case 3:
            viewReminder(bdFile);
            break;
        case 4:
            bdFile.close();
            return 0;
        default:
            std::cout << "Invalid choice. Try again.\n";
        }
    }
}
M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post