Simple Calendar App in C++

 

 

Objective:

The purpose of this program is to create a simple calendar application using C++ to manage and view events. The calendar allows users to add events for specific dates and view those events later. This application is ideal for learning basic file handling, user input processing, and time manipulation in C++.

Code:

#include 
#include 
#include 
#include 

using namespace std;

class CalendarApp {
private:
    // A map to store events by date
    map<string, string> events;

    // Function to format the date as a string
    string formatDate(int day, int month, int year) {
        return to_string(day) + "-" + to_string(month) + "-" + to_string(year);
    }

    // Function to load events from file
    void loadEvents() {
        ifstream file("events.txt");
        string date, event;
        while (getline(file, date)) {
            getline(file, event);
            events[date] = event;
        }
        file.close();
    }

    // Function to save events to file
    void saveEvents() {
        ofstream file("events.txt");
        for (auto& entry : events) {
            file << entry.first << "\n" << entry.second << "\n";
        }
        file.close();
    }

public:
    CalendarApp() {
        loadEvents();
    }

    // Function to add an event
    void addEvent(int day, int month, int year, const string& event) {
        string date = formatDate(day, month, year);
        events[date] = event;
        saveEvents();
        cout << "Event added successfully!\n";
    }

    // Function to view an event
    void viewEvent(int day, int month, int year) {
        string date = formatDate(day, month, year);
        if (events.find(date) != events.end()) {
            cout << "Event on " << date << ": " << events[date] << endl;
        } else {
            cout << "No event scheduled for this date.\n";
        }
    }
};

int main() {
    CalendarApp calendar;
    int choice;

    while (true) {
        cout << "\nSimple Calendar App\n";
        cout << "1. Add Event\n";
        cout << "2. View Event\n";
        cout << "3. Exit\n";
        cout << "Enter your choice: "; cin >> choice;

        if (choice == 1) {
            int day, month, year;
            string event;
            cout << "Enter the date (DD MM YYYY): "; cin >> day >> month >> year;
            cout << "Enter the event description: ";
            cin.ignore();  // to clear the newline character left by cin
            getline(cin, event);
            calendar.addEvent(day, month, year, event);
        } else if (choice == 2) {
            int day, month, year;
            cout << "Enter the date (DD MM YYYY): "; cin >> day >> month >> year;
            calendar.viewEvent(day, month, year);
        } else if (choice == 3) {
            break;
        } else {
            cout << "Invalid choice. Please try again.\n";
        }
    }

    return 0;
}
        

Explanation of Program Structure:

This program is structured into a class CalendarApp which handles all the functionalities of the calendar. The key methods are:

  • formatDate(int day, int month, int year): Converts the date into a string format “DD-MM-YYYY”.
  • loadEvents(): Loads existing events from the “events.txt” file.
  • saveEvents(): Saves the current events to the “events.txt” file.
  • addEvent(int day, int month, int year, const string& event): Adds a new event for a specific date.
  • viewEvent(int day, int month, int year): Displays the event for a specific date.

How to Run the Program:

  1. Open a text editor and copy the code into a new file named calendar.cpp.
  2. Ensure you have a C++ compiler installed (such as GCC or MinGW).
  3. Compile the code using the following command:
    g++ calendar.cpp -o calendar
  4. Run the compiled program using:
    ./calendar
  5. Follow the on-screen prompts to add and view events.
© 2025 Learn Programming. All rights reserved.

 

Leave a Reply

Your email address will not be published. Required fields are marked *