How To Make A Calendar Application in C++ | C++ Project

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

Home » DIY » How To Make A Calendar Application in C++ | C++ Project

Introduction Of The Project

In this coding project, we will learn How To Make A Calendar Application in C++. Printing the entire calendar for a year is very easy using this 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. 

 

Objectives

We have the following objectives while implementing this c++ source code:

  • To provide an input prompt to the user, and based on the year entered by the user, the calendar of that year will be printed.
  • The user can get the whole calendar by just mentioning the required year.

Requirements

To run this Calendar Application in C++, 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-

Source Code

#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);
}

Explanation Of The Code

The above-written code for the calendar application in c++ 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. 

Output

How To Make A Calendar Application in C++ | C++ Project

So you can see that on entering the year, we are getting the full calendar displayed on the screen. So we have successfully demonstrated how to make a Calendar Application in C++. Now you can try coding this project on your own to get more clarity on the concepts used.

 

You May Also Like To Create…