Hey everyone. I'm typing up a program which prints out a calendar for any month and year (starting with 1 A D) which the user inputs. I found a general algorithm for calculating the first day of the month but when i cout it in my if statements, something's wrong. It prints out | | 1 no matter what B is equal to. The algorithm is
1) Subtract 1 from the year and store the result in a variable A.
2) Multiply A by 365
3) Find the quotient when you divide the (year - 1) by 4 (ignore remaindr) and add this to A
4) Find the quotient when you divide the (year - 1) by 100 (ignore the remainder) and subtract this from A.
5) Find the quotient when you divide the (year - 1) by 400 (ignore the remainder) and add this to A.
6) Add the days in the year prior to this month (dont worry about leap years here) to A.
7) Add 1 to A (for first day of month)
8) If the year is a leap year and the month is after February add 1 to A. Note: A year is a leap year if it is divisible by 4 AND if it is not a centennial OR it is a centennial divisible by 400.
9) Find the remainder when dividing A by 7
Here's my code so far:
Code:
// Calendar Project
// Ez Nadar
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int day(int year, int month);
int daysbefore(int month);
int main()
{
int month = 0, year = 0, d = 0, b = 0;
cout << "Enter the year of the calendar" << endl;
cin >> year;
cout << "Enter the month of the calendar" << endl;
cin >> month;
d = daysbefore(month);
b = day(year, month);
if ( month == 12)
cout << " " << "Calendar of December " << year << endl;
if ( month == 11)
cout << " " << "Calendar of November " << year << endl;
if ( month == 10)
cout << " " << "Calendar of October " << year << endl;
if ( month == 9)
cout << " " << "Calendar of September " << year << endl;
if ( month == 8)
cout << " " << "Calendar of August " << year << endl;
if ( month == 7)
cout << " " << "Calendar of July " << year << endl;
if ( month == 6)
cout << " " << "Calendar of June " << year << endl;
if ( month == 5)
cout << " " << "Calendar of May " << year << endl;
if ( month == 4)
cout << " " << "Calendar of April " << year << endl;
if ( month == 3)
cout << " " << "Calendar of March " << year << endl;
if ( month == 2)
cout << " " << "Calendar of February " << year << endl;
if ( month == 1)
cout << " " << "Calendar of January " << year << endl;
cout << " ----------------------------------------- " << endl;
cout << " | Sun | Mon | Tue | Wed | Thu | Fri | Sat |" << endl;
cout << " ----------------------------------------- " << endl;
if ( b = 0)
cout << " | 1|";
else if ( b = 1)
cout << " | | 1|";
else if ( b = 2)
cout << " | | | 1|";
else if ( b = 3)
cout << " | | | | 1|";
else if ( b = 4)
cout << " | | | | | 1|";
else if ( b = 5)
cout << " | | | | | | 1|";
else if ( b = 6)
cout << " | | | | | | | 1|";
return 0;
}
int daysbefore(int month) // calculates days in the year before this month
{
int d = 0;
if ( month == 12)
d = 334;
if ( month == 11)
d = 304;
if ( month == 10)
d = 273;
if ( month == 9)
d = 243;
if ( month == 8)
d = 212;
if ( month == 7)
d = 181;
if ( month == 6)
d = 151;
if ( month == 5)
d = 120;
if ( month == 4)
d = 90;
if ( month == 3)
d = 59;
if ( month == 2)
d = 31;
if ( month == 1)
d = 0;
return d;
}
int day(int year, int month) // determines first day of month
{
int a = 0, b = 0;
a = year - 1;
a = a * 365;
a = a + ((year - 1) / 4);
a = a - ((year - 1) / 100);
a = a + ((year - 1) / 400);
a = a + daysbefore(month);
a = a + 1;
if ( ( year % 100 ) != 0 )
if ( (year % 4) != 0 )
{
a = a + 1;
}
b = (a%7);
return b;
}
I know the spacing is off in my cout statements