How To Make Calendar Application Project in C++

by | Dec 31, 2021 | C/C++, Coding

Introduction

In this Calendar application project, we will learn to implement the code for printing the calendar of the year entered by the user as input. The user can get the whole calendar by just mentioning the required year.

Project Use Case

It becomes very easy to print the entire calendar for a year using this particular project. Moreover, it can be used in various forms since calendars are useful tools for keeping track of upcoming meetings. They always help us to remind the important dates and schedules.

Requirements For Running The Project

To run this project, you must have installed IDE for running and compiling the C++ source code. We recommend Dev C++ or Code Blocks for a better experience.

Refer to the links given below to download the Dev C++ or Code Blocks IDE.

https://sourceforge.net/projects/embarcadero-devcpp/

https://r.search.yahoo.com/_ylt=AwrxhWbzhsRheEIA0wXnHgx.;_ylu=Y29sbwMEcG9zAzIEdnRpZAMEc2VjA3Ny/RV=2/RE=1640298356/RO=10/RU=https%3a%2f%2fsourceforge.net%2fprojects%2fcodeblocks%2f/RK=2/RS=REBPGr4TPHnFgD3uTQWl7rhbSgY-

Project Implementation

#include<iostream>
using namespace std;
int dayNumber(int day, int month, int year)
{
   static int A[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
   year -= month < 3;
   return ( year + year/4 - year/100 + year/400 + A[month-1] + day) % 7;
}
string getMonthName(int monthNumber) // to display every month in calender
{
   string M[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
   return (M[monthNumber]);
}
int numberOfDays (int monthNumber, int year)
{
   switch(monthNumber)
   {
      case 0 :
      case 2 :
      case 4 :
      case 6 :
      case 7 :
      case 9 :
      case 11: return(31); // for months of 31 days
      break;
      case 1 :
         if (year % 400 == 0 || (year % 4 == 0 && year %100 != 0))
            return (29);
         else
            return (28);
      break;
      case 3 :
      case 5 :
      case 8 :
      case 10 : return(30); // for months of 30 days
      break;
   }
}
void printCalendar(int year) // definition of printCalender function
{
   cout<<"\t\t\t Calendar - Year "<<year;
   int days;
   int current = dayNumber (1, 1, year);
   for (int i = 0; i < 12; i++)
   {
      days = numberOfDays (i, year);
      cout<<endl<<"\t\t ----X----"<<getMonthName (i).c_str()<<"----X---- \t\t"<<endl;
      cout<<" Sun  Mon  Tue   Wed    Thu    Fri    Sat \n";
      int k;
      for (k = 0; k < current; k++)
         cout<<"\t";
      for (int j = 1; j <= days; j++)
      {
         printf("%5d", j);
         if (++k > 6)
         {
            k = 0;
            cout<<endl;
         }
      }
      if (k)
         cout<<endl;
         current = k;
   }
   return;
}
int main()
{
   int year;
   cout<<"Enter the year\n";
   cin>>year; // taking year value from user as input
   printCalendar(year); // calling printCalender function 
   return (0);
}

Output

Explanation

Finally, the above-written code for calendar application has 4 functions that are:

  1. int dayNumber (int day, int month, int year)
  2. string getMonthName(int monthNumber)
  3. int numberOfDays (int monthNumber, int year)
  4. void printCalendar(int year)

All the functions have their respective arguments which are passed in them for their implementation.

  1. dayNumber:  is the function that is used to print the day number.
  2. getMonthName: is the function to print the month name one by one.
  3. numberOfDays: is the function to check for the number of days in the month and then print the calendar accordingly.
  4. printCalendar: is the function that calls the other functions and finally prints the calendar for the year.

In the main function, the user is asked to enter the ‘year’ value, and then ‘printCalendar’ function is called bypassing the ‘year’ variable. 

 

You May Also Like To Create…

0 Comments

Submit a Comment

Your email address will not be published.