What's new

C++ Calendar Code Snippet

C J B

Enthusiast
Joined
Jan 9, 2023
Posts
76
Reaction
19
Points
35
#include <iostream>
#include <iomanip>
#include <ctime>

using namespace std;

int main() {
int year;
cout << "Enter a year: ";
cin >> year;

// Set the tm struct to January 1 of the desired year
tm time_in = { 0, 0, 0, 1, 0, year - 1900 };

// Convert tm struct to time_t and back to tm
time_t time_temp = mktime(&time_in);
tm *time_out = localtime(&time_temp);

// Calculate the number of days in the year
int days_in_year = 365 + (time_out->tm_yday == 365 && time_out->tm_mon > 1);

// Set the width of the day name column
cout << setw(10);

// Print header row with the name of each day
cout << "Sun Mon Tue Wed Thu Fri Sat" << endl;

// Calculate the number of spaces to leave before the first day of the year
int spaces = time_out->tm_wday;
cout << setw(spaces * 3) << "";

// Loop through all days of the year
for (int day = 1; day <= days_in_year; ++day) {
cout << setw(2) << day << " ";

// Start a new line after every 7 days
if ((day + spaces) % 7 == 0) {
cout << endl;
}
}

return 0;
}
 
This code takes a year input from the user and prints out a calendar for that year in the console. Here's how it works:

1. The user is prompted to enter a year.
2. A tm struct is set to represent January 1 of that year.
3. The mktime() function is called with the tm struct as an argument to convert it to an time_t object representing that date and time.
4. The localtime() function is called with the time_t object as an argument to convert it back to a tm struct, adjusting for the local time zone.
5. The number of days in the year is calculated based on whether it is a leap year (365 days by default, 366 days in a leap year).
6. The header row of the calendar is printed with the names of each day of the week.
7. The number of spaces to leave at the beginning of the first row is calculated based on which day of the week January 1 falls on.
8. Starting with day 1, each day of the year is printed with its day number and a space.
9. If the day being printed plus the number of spaces that were left at the beginning of the first row is a multiple of 7, a new line is started.

Overall, this code provides a simple way to generate a calendar for any given year.
 
This is a C++ program that takes a year as input and prints out a calendar for that year. Here is a breakdown of the program:
  • The user is prompted to enter a year.
  • A tm struct named time_in is created and initialized to January 1 of the desired year. This is done by setting the tm_year member to year - 1900 and leaving all other members at their default values (i.e., 0).
  • The time_in struct is converted to a time_t value using the mktime function, and then converted back to a tm struct named time_out using the localtime function. This is done to normalize the tm struct and ensure that all fields are valid.
  • The number of days in the year is calculated using the formula 365 + (time_out->tm_yday == 365 && time_out->tm_mon > 1). This formula adds an extra day to the year if it is a leap year (i.e., if the year is divisible by 4 but not by 100, or if it is divisible by 400).
  • The header row of the calendar is printed, with the name of each day of the week.
  • The number of spaces to leave before the first day of the year is calculated using the tm_wday member of the time_out struct, which gives the day of the week for January 1. This value is multiplied by 3 (the width of each day column) to determine how many spaces to print before the first day.
  • A loop is started that iterates over all the days of the year, starting from day 1. For each day, the day number is printed using setw(2) to ensure that it takes up two characters. The setw function sets the width of the field that the next output will be printed in. After the day number is printed, a space is printed to separate it from the next day.
  • A check is made to see if the current day is the last day of the week (i.e., if (day + spaces) % 7 == 0). If it is, a newline is printed to start a new row. This is done using the endl manipulator, which inserts a newline character and flushes the output buffer.
  • The loop continues until all days of the year have been printed.
Overall, this program demonstrates how to use the tm struct and various time-related functions in C++ to print out a calendar for a given year.
 

Similar threads

Back
Top