I'm new to C/C++ and I'm having difficulty with calculating leap years in my Zeller's Algorithm program (based off of the Gregorian Calendar). I've lost sleep over this code for the last week and this is my final breaking point.
I continually get "This day is Friday." for input 1 16 2012 ... which should be Monday instead.. etc, etc.
If anyone could help or give me hints, that would be excellent.
Here is my code so far:
#include<iostream>
using namespace std;
void DisplayTitle();
// displays the title
void GetDate(int&, int&, int&);
// collects the user's input and/or terminates the program
int CalcDayOfWeek();
// returns the value for G --> calculates the day of the week
void DisplayDay(int);
// displays what day G represents
int month, dayOfMonth, year;
int main()
{
//int M, K, C, D; ---- don't need these
int G;
while(1)
{
DisplayTitle();
GetDate(month, dayOfMonth, year);
G = CalcDayOfWeek() % 7;
DisplayDay(G);
}
system("pause");
return 0;
}
// DisplayTitle Function --- test: WORKS
void DisplayTitle()
{
cout << "\n Zeller's Algorithm" <<endl;
}
// GetDate Call-by-reference Function --- tested: WORKS
void GetDate(int& month, int& dayOfMonth, int& year)
{
cout << "\n Enter month (or 0 to exit) ";
cin >> month;
if (month==0)
exit(1);
cout << "\n Enter day ";
cin >> dayOfMonth;
cout << "\n Enter year ";
cin >> year;
}
//CalcDayOfWeek Function --- test:
int CalcDayOfWeek()
{
double G;
int K = dayOfMonth;
int C = year / 100;
int D = year % 100;
int M = month - 2;
// January of a leap year --- that is NOT a century mark such as 1600, 1800, 2000; --- test:
if((year % 400 == 0)&&(month < 2)&&(D != 0))
{
M = month + 10;
D -= 1;
G = (static_cast<int>(2.6 * M - 0.2) + K + D + (D/4.0) + (C/4.0) + 2 * C);
}
// February of a leap year (before the 29th) --- that is NOT a century mark --- test:
if ((year % 400 == 0)&&(month == 2)&&(K < 29)&&(D !=0))
{
M = month + 10;
D -= 1;
G = static_cast<int>((2.6 * M - 0.2) + K + D + (D/4.0) + (C/4.0) + 2 * C) ;
//G -= 4; testing this out..
}
if(month <= 2)
{
M = month + 10;
D -= 1;
}
G = static_cast<int>((2.6 * M - 0.2) + K + D + (D/4.0) + (C/4.0) + 2 * C);
if(G < 0)
G +=7;
if(G > 6)
G -= 7;
cout << "M = " << M <<endl;
cout << "K = " << K <<endl;
cout << "C = " << C <<endl;
cout << "D = " << D <<endl;
cout << "G = " << G <<endl <<endl;
return(static_cast<int>(G));
}
// DisplayDay Function --- test:
void DisplayDay(int day)
{
cout << endl <<endl << " This day is ";
switch(day)
{
case '0':
cout << "Sunday.";
break;
case 1:
cout << "Monday.";
break;
case 2:
cout << "Tuesday.";
break;
case 3:
cout << "Wednesday.";
break;
case 4:
cout << "Thursday.";
break;
case 5:
cout << "Friday.";
break;
case 6:
cout << "Saturday.";
break;
}
cout <<endl <<endl;
}