How to print a calendar for a specific month and year in C++

2 Answers

0 votes
#include <iostream>
#include <chrono>
#include <iomanip>
#include <vector>

//using namespace std;
using namespace std::chrono;

void printMonthCalendar(int year_val, int month_val) {
    const std::vector<std::string> months = {"January", "February", "March", "April", "May", "June", 
                                             "July", "August", "September", "October", "November", "December"};
    
    // 1. Create a year_month object
    year y{year_val};
    month m{(unsigned)month_val};
    year_month ym{y, m};

    // 2. Print Header
    std::cout << "\n      " << months[month_val - 1] << " " << year_val << "\n";
    std::cout << " Sun Mon Tue Wed Thu Fri Sat\n";

    // 3. Get first day of the month and its weekday
    year_month_day first_day_ymd{y / m / 1};
    weekday first_wd{sys_days{first_day_ymd}};
    int start_pos = first_wd.c_encoding(); // 0 = Sun, 1 = Mon...

    // 4. Get total days in the month using C++20 'last' constant
    auto last_day_val = (y / m / last).day();
    unsigned total_days = (unsigned)last_day_val;

    // 5. Print leading spaces for alignment
    for (int i = 0; i < start_pos; i++) std::cout << "    ";

    // 6. Print days
    for (unsigned d = 1; d <= total_days; d++) {
        std::cout << std::setw(4) << d;
        if ((start_pos + d) % 7 == 0) std::cout << "\n";
    }
    std::cout << "\n";
}

int main() {
    int m = 1, y = 2026;
    
    printMonthCalendar(y, m);
}




/*
run:

      January 2026
 Sun Mon Tue Wed Thu Fri Sat
                   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

*/



 



answered Jan 18 by avibootz
0 votes
#include <iostream>
#include <chrono>
#include <format> // Required for std::format

using namespace std::chrono;

void printMonthCalendar(int y, unsigned m) {
    // 1. Initialize year_month
    year_month ym = year{y} / month{m}; 

    // 2. Get first day and convert to sys_days to find the weekday
    year_month_day first_ymd = ym / day{1};
    weekday wd = weekday{sys_days{first_ymd}}; // Fix: convert to sys_days first

    // 3. Get total days in month
    year_month_day_last last_ymd = ym / last;
    unsigned days = unsigned(last_ymd.day());

    // 4. Print Header
    // Note: Using standard cout for wider compatibility if std::format chrono-specifiers vary
    std::cout << "     " << (unsigned)m << " - " << y << "\n";
    std::cout << "Su Mo Tu We Th Fr Sa\n";

    // 5. Print leading spaces for the first week
    for (unsigned i = 0; i < wd.c_encoding(); ++i)
        std::cout << "   ";

    // 6. Print the days of the month
    for (unsigned d = 1; d <= days; ++d) {
        std::cout << std::format("{:2} ", d);
        
        // Wrap to next line after Saturday (encoding 6)
        if ((wd.c_encoding() + d) % 7 == 0)
            std::cout << '\n';
    }
    std::cout << '\n';
}

int main() {
    int m = 1, y = 2026;
    
    printMonthCalendar(y, m);
}




/*
run:

     1 - 2026
Su Mo Tu We Th Fr Sa
             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

*/



 



answered Jan 18 by avibootz

Related questions

...