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
, andviewReminder
encapsulate specific functionalities.
Birthday Reminder Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | #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"; } } } |