Calendar App in C

 

This simple calendar app allows you to add and view events for specific dates. It is a text-based application that makes use of the C programming language for building and managing events.

Objective

The objective of this project is to create a simple command-line-based calendar application using the C programming language. This application will enable users to add and view events for specific dates, providing basic calendar functionalities. The main goal is to demonstrate how to manage date-related data and interact with the user through a command-line interface.

Program Code

#include 
#include 
#include 

#define MAX_EVENTS 100
#define DATE_LENGTH 11
#define EVENT_LENGTH 100

// Struct to store event information
typedef struct {
    char date[DATE_LENGTH];
    char event[EVENT_LENGTH];
} Event;

// Array to store events
Event calendar[MAX_EVENTS];
int event_count = 0;

// Function to add an event to the calendar
void add_event() {
    if (event_count >= MAX_EVENTS) {
        printf("Event list is full.\n");
        return;
    }

    char date[DATE_LENGTH];
    char event[EVENT_LENGTH];

    printf("Enter the date for the event (YYYY-MM-DD): ");
    scanf("%s", date);
    getchar(); // to consume the newline character left by scanf

    printf("Enter the event description: ");
    fgets(event, EVENT_LENGTH, stdin);
    event[strcspn(event, "\n")] = '\0'; // Remove newline character from event

    // Store the event in the calendar
    strcpy(calendar[event_count].date, date);
    strcpy(calendar[event_count].event, event);
    event_count++;

    printf("Event added successfully!\n\n");
}

// Function to view events for a specific date
void view_events() {
    char date[DATE_LENGTH];
    int found = 0;

    printf("Enter the date to view events (YYYY-MM-DD): ");
    scanf("%s", date);

    printf("\nEvents for %s:\n", date);
    for (int i = 0; i < event_count; i++) {
        if (strcmp(calendar[i].date, date) == 0) {
            printf("- %s\n", calendar[i].event);
            found = 1;
        }
    }

    if (!found) {
        printf("No events found for this date.\n");
    }
    printf("\n");
}

// Function to display the main menu
void display_menu() {
    printf("Calendar Application Menu:\n");
    printf("1. Add Event\n");
    printf("2. View Events\n");
    printf("3. Exit\n");
    printf("Enter your choice: ");
}

int main() {
    int choice;

    // Main program loop
    while (1) {
        display_menu();
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                add_event();
                break;
            case 2:
                view_events();
                break;
            case 3:
                printf("Exiting program...\n");
                exit(0);
            default:
                printf("Invalid choice, please try again.\n\n");
        }
    }

    return 0;
}

Explanation of the Program Structure

This program is structured into a few key sections:

  • Event Structure: The Event struct stores the date and description of an event.
  • Event Array: The calendar array holds multiple events, with a maximum of MAX_EVENTS (100 events).
  • Functions: The program contains functions to add events, view events, and display a simple menu.
  • Main Program Loop: The program runs a loop that continuously displays a menu and allows the user to perform actions until the program is exited.

The key functionalities include:

  1. add_event: This function allows the user to input a date and an event description, which is then stored in the calendar array.
  2. view_events: This function allows the user to input a date and view all events scheduled for that specific day.
  3. display_menu: This function displays the options for the user and captures their input to execute the corresponding action.

How to Run the Program

Follow these steps to run the program:

  1. Install a C compiler (e.g., GCC).
  2. Create a new C file (e.g., calendar.c) and copy the program code into the file.
  3. Open a terminal or command prompt and navigate to the directory containing the calendar.c file.
  4. Compile the program using the command: gcc calendar.c -o calendar
  5. Run the compiled program using: ./calendar
  6. Follow the menu prompts to add and view events.
© 2025 Learn Programming

 

Leave a Reply

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