How to print calendar for a specific year in C++
This tutorial will help you to learn how to print calendar for a specific year in C++. The user can get the whole calendar by just mentioning the required year. Feel free to ask questions or leave comments in the comments section at the end of the post.
Print calendar for a specific year in C++
Let us go through each function used.
1. Libraries used
Code :
#include <iostream> #include <cstdlib> #include <iomanip>
iostream : basic input and output
cstdlib : this is a general utilities library. Various integer arithmetics, memory management dynamic functions, etc are included in the library.
iomanip : the functions in this library are used to manipulate the output of the C++ programs.
2. Leap Year
Code :
bool leap_year (int year) { return ((year%4==0) && (year%100 !=0))||(year%400==0) ; }
leap_year : This function checks if the entered year is a leap year or not using the formula.
3. Month Functions
Code :
int start_day(int year) { int day_start; int x1, x2, x3; x1 = (year - 1)/ 4; x2 = (year - 1)/ 100; x3 = (year - 1)/ 400; day_start = (year + x1 - x2 + x3) %7; return day_start; } int number_days_month (int m, bool leap) { if (m == 1) return(31); else if (m == 2) if (leap) return(29);else return(28); else if (m == 3) return(31); else if (m == 4) return(30); else if (m == 5) return(31); else if (m == 6) return(30); else if (m == 7) return(31); else if (m == 8) return(31); else if (m == 9) return(30); else if (m == 10) return(31); else if (m == 11) return(30); else if (m == 12) return(31); else error(); } void print_month_name (int m) { if (m == 1) { skip(7); cout << "January" << "\n"; } else if (m == 2) { skip(7); cout << "February" << "\n"; } else if (m == 3) { skip(7); cout << "March" << "\n"; } else if (m == 4) { skip(7); cout << "April" << "\n"; } else if (m == 5) { skip(7); cout << "May" << "\n"; } else if (m == 6) { skip(7); cout << "June" << "\n"; } else if (m == 7) { skip(7); cout << "July" << "\n"; } else if (m == 8) { skip(7); cout << "August" << "\n"; } else if (m == 9) { skip(7); cout << "September" << "\n"; } else if (m == 10) { skip(7); cout << "October" << "\n"; } else if (m == 11) { skip(7); cout << "November" << "\n"; } else if (m == 12) { skip(7); cout << "December" << "\n"; } else error(); cout << " S M T W T F S" <<"\n"; cout << "____________________" << "\n"; } void print_month (int number_days, int &weekDay) { int day = 1; while (day <= number_days) { cout << setw(2) << day << " "; if (weekDay == 6) { cout << "\n"; weekDay = 0; } else weekDay = weekDay + 1; day = day + 1; } }
start_day() : This function calculates and returns the start day of the year specified.
number_days_month() : The number of days depending on the month entered are calculated and returned by this function.
print_month_name() : For presenting the calendar, this function prints the month name.
print_month() : This function actually prints the date numbers below the month title for each month.
As you can see, all the calculations are are done using simple formulas. You will have to look at all these formulas closely to understand how they work.
(Note : setw() function is used to set the width for the field)
4. Other functions
Code :
void skip (int i) { while (i > 0) { cout << " "; i = i - 1; } } void error () { cout << "\nError. End procedure." << "\n"; exit ( -1); }
skip() : This function is used to add spaces and make the output presentable in calender format.
error() : This function is used to handle cases of incorrect values respective to the problem and stop the execution (eg month > 12 or month < 1)
5. Variables and Function calls
Code :
int year, start_day_month, number_days, current_month = 1; bool leap; cout << "Enter the year : "; cin >>year; start_day_month=start_day(year); leap = leap_year(year); skip(9); cout << year << "\n"; while (current_month <= 12) { number_days = number_days_month(current_month, leap); print_month_name(current_month); print_month(number_days, start_day_month); cout << "\n\n\n"; current_month = current_month + 1; }
The user input as the specific year is taken and stored in variable ‘year’.
The ‘for’ loop is run for 12 times (once for each month) and for each iteration the functions are called to calculate number of days for the month, print month name and print the month dates.
Complete Code to print calendar of a specific year in C++
#include <iostream> #include <cstdlib> #include <iomanip> using namespace std; void skip (int i) { while (i > 0) { cout << " "; i = i - 1; } } void error () { cout << "\nError. End procedure." << "\n"; exit ( -1); } bool leap_year (int year) { return ((year%4==0) && (year%100 !=0))||(year%400==0) ; } int start_day(int year) { int day_start; int x1, x2, x3; x1 = (year - 1)/ 4; x2 = (year - 1)/ 100; x3 = (year - 1)/ 400; day_start = (year + x1 - x2 + x3) %7; return day_start; } int number_days_month (int m, bool leap) { if (m == 1) return(31); else if (m == 2) if (leap) return(29);else return(28); else if (m == 3) return(31); else if (m == 4) return(30); else if (m == 5) return(31); else if (m == 6) return(30); else if (m == 7) return(31); else if (m == 8) return(31); else if (m == 9) return(30); else if (m == 10) return(31); else if (m == 11) return(30); else if (m == 12) return(31); else error(); } void print_month_name (int m) { if (m == 1) { skip(7); cout << "January" << "\n"; } else if (m == 2) { skip(7); cout << "February" << "\n"; } else if (m == 3) { skip(7); cout << "March" << "\n"; } else if (m == 4) { skip(7); cout << "April" << "\n"; } else if (m == 5) { skip(7); cout << "May" << "\n"; } else if (m == 6) { skip(7); cout << "June" << "\n"; } else if (m == 7) { skip(7); cout << "July" << "\n"; } else if (m == 8) { skip(7); cout << "August" << "\n"; } else if (m == 9) { skip(7); cout << "September" << "\n"; } else if (m == 10) { skip(7); cout << "October" << "\n"; } else if (m == 11) { skip(7); cout << "November" << "\n"; } else if (m == 12) { skip(7); cout << "December" << "\n"; } else error(); cout << " S M T W T F S" <<"\n"; cout << "____________________" << "\n"; } void print_month (int number_days, int &weekDay) { int day = 1; while (day <= number_days) { cout << setw(2) << day << " "; if (weekDay == 6) { cout << "\n"; weekDay = 0; } else weekDay = weekDay + 1; day = day + 1; } } int main () { int year, start_day_month, number_days, current_month = 1; bool leap; cout << "Enter the year : "; cin >>year; cout<<"\n"; start_day_month=start_day(year); leap = leap_year(year); skip(9); cout << year << "\n"; while (current_month <= 12) { number_days = number_days_month(current_month, leap); print_month_name(current_month); print_month(number_days, start_day_month); cout << "\n\n\n"; current_month = current_month + 1; } cout << "\n"; }
Output :
You may also learn,
How to find day from date in C++
How to add days to date in C++
how to add a reminder to a particular day from the calendar