C++ Calendar with Colorful Text Output

Learn how to create C++ Calendar with Colorful Text Output. This program generates calendars for specific months or entire years, featuring leap year calculations, day-of-the-week determination, and C++ Calendar with Colorful Text Output for a visually appealing experience.


1. How to Use Windows.h for Console Colors in C++

The program uses Windows-specific functions to manipulate Colorful Console Output in C++. Two key functions are responsible for this:

  • Set Text Color: This function changes the color of the text displayed in the console. It takes a color code as input and applies it to the console output.
  • Reset Text Color: This function reverts the text color to the default (usually white or gray). It ensures that the console returns to its standard appearance after applying custom colors.

These functions are used throughout the program to highlight headers, days of the week, and specific dates (like Sundays and the first day of the month).

// Function to set text color (Windows-specific)
void setColor(int color) {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}

// Function to reset text color to default
void resetColor() {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); // 7 is default color
}

  • You can get Source code of C++ Calendar with Colorful Text Output from my GitHub repository
  • You can also download the zip file

2. Leap Year Calculation

The program includes a function to determine whether a given year is a leap year. This is essential for correctly displaying the number of days in February. The logic follows the standard leap year rules:

  • A year is a leap year if it is divisible by 4.
  • However, if the year is divisible by 100, it is not a leap year unless it is also divisible by 400.

This function ensures that the calendar accurately accounts for leap years.

// Function to check if a year is a leap year
bool isLeapYear(int year) {
    return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}

3. C++ Program to Display Month and Year Calendar

Another function calculates the number of days in a given month and year. The logic is as follows:

  • February has 28 days (or 29 in a leap year).
  • April, June, September, and November have 30 days.
  • All other months have 31 days.

This function is used to determine how many days to display for each month in the calendar.

// Function to get the number of days in a month
int getDaysInMonth(int month, int year) {
    if (month == 2) {
        return isLeapYear(year) ? 29 : 28;
    } else if (month == 4 || month == 6 || month == 9 || month == 11) {
        return 30;
    } else {
        return 31;
    }
}

4. C++ Calendar Generator with Zeller’s Congruence

The program uses a mathematical formula (Zeller’s Congruence) to determine the day of the week for the first day of a given month and year. This is crucial for aligning the calendar correctly. The function adjusts the month and year for January and February (treating them as months 13 and 14 of the previous year) and then computes the day of the week (0 = Saturday, 1 = Sunday, …, 6 = Friday).

int getDayOfWeek(int month, int year) {
    if (month < 3) {
        month += 12;
        year -= 1;
    }
    int k = year % 100;
    int j = year / 100;
    int day = 1 + (13 * (month + 1)) / 5 + k + (k / 4) + (j / 4) + 5 * j;
    return day % 7;
}

5. Printing a Single Month’s Calendar

The program includes a function to print a formatted calendar for a specific month and year. Here’s how it works:

  1. Header: The month and year are displayed in a colorful header.
  2. Days of the Week: The days of the week (Sun, Mon, Tue, etc.) are printed in a different color for clarity.
  3. Calendar Grid: The function calculates the starting day of the week and prints the dates in a grid format. Sundays and the first day of the month are highlighted in green, while other days use the default color.
  4. Alignment: The function ensures that the dates are properly aligned under the correct days of the week.
C++ Calendar with Colorful Text Output

6. Printing an Entire Year’s Calendar

The program can also print a calendar for an entire year. It does this by calling the single-month calendar function for each month (from January to December) and displaying them sequentially. This provides a comprehensive view of the entire year.

void printYearCalendar(int year) {
    for (int month = 1; month <= 12; ++month) {
        printMonthCalendar(month, year);
        cout << endl;
    }
}
C++ Calendar with Colorful Text Output

7. Design a Menu-Driven Calendar in C++

The program features a user-friendly menu that allows users to interact with the calendar generator. The menu offers the following options:

  1. View a Specific Month: Users can input a month and year to generate a calendar for that specific month.
  2. View the Entire Year: Users can input a year to generate a calendar for all 12 months of that year.
  3. Exit: Users can exit the program.

The menu is designed with colorful prompts and includes error handling to ensure that invalid inputs (e.g., an invalid month number) are gracefully managed.


8. Main Program Loop

The program runs in a loop, continuously displaying the menu and processing user input until the user chooses to exit. This ensures that users can generate multiple calendars without restarting the program.


Key Features and Benefits

  1. Colorful Output: The use of colors makes the calendar visually appealing and easy to read.
  2. Accuracy: The program accurately handles leap years and correctly aligns dates with the days of the week.
  3. User Interaction: The interactive menu makes the program accessible and user-friendly.
  4. Flexibility: Users can generate calendars for specific months or entire years, making the tool versatile.

Why This Program is Useful

  • Practical Application: The calendar generator can be used for personal planning, scheduling, or educational purposes.
  • Learning Opportunity: The program demonstrates key programming concepts, including functions, loops, conditional statements, and user input handling. It also introduces platform-specific features like console color manipulation.
  • Customization Potential: The program can be extended with additional features, such as highlighting holidays, adding event reminders, or exporting the calendar to a file.

Conclusion of C++ Calendar with Colorful Text Output

The Advanced Calendar Generator is a well-structured and functional program that combines practicality with creativity. By leveraging mathematical formulas, user interaction, and colorful formatting, it provides a useful tool for generating calendars while showcasing essential programming techniques. Whether you’re a beginner or an experienced programmer, this project offers valuable insights and opportunities for further exploration.


Calendar Generator with Source Code in C++

#include <iostream>
#include <iomanip>
#include <windows.h> // For Windows-specific color functions
using namespace std;

// Function to set text color (Windows-specific)
void setColor(int color) {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}

// Function to reset text color to default
void resetColor() {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); // 7 is default color
}

// Function to check if a year is a leap year
bool isLeapYear(int year) {
    return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}

// Function to get the number of days in a month
int getDaysInMonth(int month, int year) {
    if (month == 2) {
        return isLeapYear(year) ? 29 : 28;
    } else if (month == 4 || month == 6 || month == 9 || month == 11) {
        return 30;
    } else {
        return 31;
    }
}

// Function to get the day of the week for the first day of the month
int getDayOfWeek(int month, int year) {
    if (month < 3) {
        month += 12;
        year -= 1;
    }
    int k = year % 100;
    int j = year / 100;
    int day = 1 + (13 * (month + 1)) / 5 + k + (k / 4) + (j / 4) + 5 * j;
    return day % 7;
}

// Function to print a single month's calendar
void printMonthCalendar(int month, int year) {
    string months[] = {"January", "February", "March", "April", "May", "June", 
                       "July", "August", "September", "October", "November", "December"};
    
    int daysInMonth = getDaysInMonth(month, year);
    int dayOfWeek = getDayOfWeek(month, year);

    // Print the header
    setColor(14); // Yellow for header
    cout << "\n-------------------------------------" << endl;
    cout << "           " << months[month - 1] << " " << year << endl;
    cout << "-------------------------------------" << endl;
    resetColor();

    setColor(11); // Cyan for days of the week
    cout << " Sun  Mon  Tue  Wed  Thu  Fri  Sat" << endl;
    resetColor();

    // Print the calendar
    for (int i = 0; i < dayOfWeek; ++i) {
        cout << "     ";
    }

    for (int day = 1; day <= daysInMonth; ++day) {
        if ((day + dayOfWeek - 1) % 7 == 0 || day == 1) {
            setColor(10); // Green for Sundays and the first day
        } else {
            setColor(7); // Default color for other days
        }
        printf("%4d ", day);
        if ((day + dayOfWeek) % 7 == 0 || day == daysInMonth) {
            cout << endl;
        }
    }
    resetColor();
}

// Function to print the entire year's calendar
void printYearCalendar(int year) {
    for (int month = 1; month <= 12; ++month) {
        printMonthCalendar(month, year);
        cout << endl;
    }
}

// Main menu function
void displayMenu() {
    setColor(13); // Magenta for menu
    cout << "-------------------------------------" << endl;
    cout << "       ADVANCED CALENDAR GENERATOR   " << endl;
    cout << "-------------------------------------" << endl;
    cout << "1. View a specific month" << endl;
    cout << "2. View the entire year" << endl;
    cout << "3. Exit" << endl;
    cout << "-------------------------------------" << endl;
    resetColor();
    cout << "Enter your choice: ";
}

int main() {
    int choice, month, year;

    while (true) {
        displayMenu();
        cin >> choice;

        switch (choice) {
            case 1:
                cout << "Enter month (1-12): ";
                cin >> month;
                if (month < 1 || month > 12) {
                    setColor(12); // Red for error
                    cout << "Invalid month! Please enter a month between 1 and 12." << endl;
                    resetColor();
                    break;
                }
                cout << "Enter year: ";
                cin >> year;
                printMonthCalendar(month, year);
                break;

            case 2:
                cout << "Enter year: ";
                cin >> year;
                printYearCalendar(year);
                break;

            case 3:
                setColor(14); // Yellow for exit message
                cout << "Exiting the program. Goodbye!" << endl;
                resetColor();
                return 0;

            default:
                setColor(12); // Red for invalid choice
                cout << "Invalid choice! Please select a valid option." << endl;
                resetColor();
                break;
        }

        cout << "\nPress Enter to continue...";
        cin.ignore();
        cin.get();
    }

    return 0;
}

Leave a comment