A calender App in C++

I am designing a calender App in c++ that functions upon the year input.
I got a sample code online and i am trying to understand it but i am stuck somewhere.

PLEASE I NEED HELP.
I dont understand from the line that has n=(year_2nd-1)*(365+0.25-0.01+0.0025);
n++;


THIS IS THE CODE FOR THE CALENDER

#include<iostream>
#include<conio.h>

using namespace std;

int showmonth(int month,int year,int n);
int leapyear();

int main()
{

//First part of the application determines wheter the chosen year is a leap year or not.

cout<<"\t\t................................................"<<endl;
cout<<"\t\t\t A CALENDER APPLICATION IN C++"<<endl;
cout<<"\t\t\t PROGRAMMED BY GROUP 1"<<endl;
cout<<"\t\t................................................"<<endl;

cout<<"\n";

leapyear();

cout<<"\n";

cout<<"Now you can enter a year to generate it's calender : ";
char x;//this character is used in repeating the use of the application

x='y';//x would be invoked upon keying y and this y is an input character

while(x=='y')

{

int n,year_2nd;//NB: y has been used as a variable(year) here
//cout<<"Enter any year :- "<<endl;
cin>>year_2nd;

cout<<"\n\n\n\t\tCALENDAR OF THE YEAR "<<year_2nd<<endl;

I DONT UNDERSTAND THIS PART.

n=(year_2nd-1)*(365+0.25-0.01+0.0025);
n++;

for(int month_number=1;month_number<=12;month_number++)

{

n=showmonth(month_number,year_2nd,n);
}

}
getch();
return 0;
}

int showmonth(int month,int y,int n)
{
int d=1,s=1,numberOfdays=31,p;
switch(month)
{
case 1 :cout<<"\nJANUARY\n";
numberOfdays=31;
break;
case 2 :cout<<"\nFEBRUARY\n";
if(((y%4==0)||(y%100==0))||(y%400==0))
numberOfdays=29;
else
numberOfdays=28;
break;
case 3: cout<<"\nMARCH\n";
numberOfdays=31;
break;
case 4: cout<<"\nAPRIL\n";
numberOfdays=30;
break;
case 5:cout<<"\nMAY\n";
numberOfdays=31;
break;
case 6:cout<<"\nJUNE\n";
numberOfdays=30;
break;
case 7:cout<<"\nJULY\n";
numberOfdays=31;
break;
case 8:cout<<"\nAUGUST\n";
numberOfdays=31;
break;
case 9:cout<<"\nSEPTEMBER\n";
numberOfdays=30;
break;
case 10:cout<<"\nOCTOBER\n";
numberOfdays=31;
break;
case 11:cout<<"\nNOVEMBER\n";
numberOfdays=30;
break;
case 12:cout<<"\nDECEMBER\n";
numberOfdays=31;
break;
}

cout<<endl<<y<<endl;
n=n%7;



cout<<"\n-----------------------------------------------------\n";
cout<<"SUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT\t\n";
cout<<"\n-----------------------------------------------------\n";

while(s<=n)

{
cout<<"\t";
s++;
}

while((n<7)&&(d<=numberOfdays)){while((n<7)&&(d<=numberOfdays))
{
cout<<d<<"\t";
n++;
d++;
}
cout<<"\n";

p=n;
n=0;
}
cout<<"\n-----------------------------------------------------\n";
return(p);
}

int leapyear(){
int year_1st;

cout<<"Please key in the calender's year and press the ENTER key to continue.... "<<endl;
cin>>year_1st;

if(year_1st%4==0){
if(year_1st%100==0){
if(year_1st%400==0);
}
cout<<"\t"<<year_1st<<" is a leap year !@@!";

}
else
cout<<"\t"<<year_1st<<" is not a leap year !@@!";
//return 0;
}
Last edited on
It's an obfuscated (and poorly-written) calculation for the day of the week that the first of the month falls on.

Here's something better:

1
2
3
4
5
6
7
8
9
10
11
12
//----------------------------------------------------------------------------
// http://www.faqs.org/faqs/calendars/faq/part1/index.html
//
unsigned day_of_week( unsigned year, unsigned month, unsigned day )
  {
  unsigned a, y, m;
  a = (14 - month) / 12;
  y = year - a;
  m = month + (12 * a) - 2;
  // Gregorian:
  return (day + y + (y / 4) - (y / 100) + (y / 400) + ((31 * m) / 12)) % 7;
  }

Hope this helps.
Last edited on
Thanks for the quick reply i am so grateful. Can you please help me understand your code and also how can i integrate it with the one i posted earlier. I am very young in programming.
Last edited on
The code I gave you is just a function. Copy'n'paste it, and call it with the correct argument values.

It is nothing more than a well-known mathematical calculation to determine the day of week the first of a month falls on in the (Modified) Gregorian calendar, according to the reading found at the link I gave you.

Hope this helps.
Thanks so much. I am to give it a try. I would give you a reply soon
Topic archived. No new replies allowed.