Introduction
A personal diary is a great way to keep track of your thoughts, memories, and experiences.
This simple C++ diary application allows you to write, save, and view daily journal entries.
The program lets you create multiple entries, stores them in a text file, and ensures your diary remains safe and easy to access.
Objective: The goal of this program is to provide users with a simple, efficient way to maintain a digital diary. It allows users to write daily entries, save them with a timestamp, and retrieve them as needed, all through a console-based C++ program.
Code
#include #include #include #include using namespace std; void writeEntry() { ofstream diaryFile; string entry; // Open diary file in append mode diaryFile.open("diary.txt", ios::app); if (!diaryFile) { cout << "Error opening diary file!" << endl; return; } // Get current date and time time_t now = time(0); char* dt = ctime(&now); // Write date and entry to file diaryFile << "Date: " << dt; cout << "Enter your diary entry (type 'EXIT' to stop writing):" << endl; // Get user input for diary entry while (true) { getline(cin, entry); if (entry == "EXIT") break; diaryFile << entry << endl; } diaryFile << "------------------------------------\n\n"; // Separation between entries diaryFile.close(); cout << "Your entry has been saved!" << endl; } void readEntries() { ifstream diaryFile; string line; diaryFile.open("diary.txt"); if (!diaryFile) { cout << "Diary is empty or file not found!" << endl; return; } cout << "\nDiary Entries:\n"; while (getline(diaryFile, line)) { cout << line << endl; } diaryFile.close(); } int main() { int choice; while (true) { cout << "\nC++ Diary Application" << endl; cout << "1. Write a new entry" << endl; cout << "2. Read previous entries" << endl; cout << "3. Exit" << endl; cout << "Enter your choice: "; cin >> choice; cin.ignore(); // To clear the newline character from input buffer if (choice == 1) { writeEntry(); } else if (choice == 2) { readEntries(); } else if (choice == 3) { cout << "Exiting the program. Have a nice day!" << endl; break; } else { cout << "Invalid choice. Please try again." << endl; } } return 0; }
Program Structure and Explanation
This diary application is a simple console-based C++ program that offers two main functionalities: writing new diary entries and reading previous ones.
- writeEntry(): This function opens the “diary.txt” file in append mode, allowing the user to add new entries. It adds a timestamp with the current date and time before each entry and stores it in the file.
- readEntries(): This function opens the “diary.txt” file in read mode and displays all the saved entries line by line.
- main(): The main function provides a simple menu with three options: to write a new entry, read previous entries, or exit the program. The user input determines which function is called.
How to Run the Program
- Ensure you have a C++ compiler installed (e.g., GCC or Visual Studio).
- Copy the code provided above into a file named diary.cpp.
- Open your terminal or command prompt, navigate to the directory where the file is located, and compile the program using the command:
g++ diary.cpp -o diary
. - Run the program using the command:
./diary
on Linux/Mac ordiary.exe
on Windows. - Follow the on-screen menu to write or read diary entries.