Program to calculate a month calendar for any month and year?

Oct 25, 2008 at 3:59am
This is what i have so far. i can't get the floor value to work. and i have to use the ibm ascii set. How do i solve those to problems?


#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
int main ()

{
int year,month,firstday,leapsum, days;





cout<<"Please enter the number of the month and year to access calendar\n"<<endl;

cout<< "Please leave a space in between the month and year when you enter them: \n";

cin>>month>>year;

firstday=(((year-1)*365)+4*((year-1)/4)-4*((year-1)/100)+4*((year-1)/400)+1)%7;

leapsum=(!(year%4)&&(year%100))||!(year%400);

day

system("cls");



switch (month)
{case 1:
month;
{if (leapsum=0)
days=31;
else
days=31;}
cout<<"January"<<" "<<firstday<<" "<<year;
break;

case 2:
month;
{if (leapsum=0)
days=29;
else
days=28;}
cout<<"February"<<" "<<firstday<<" "<<year;
break;

case 3:
month;
{if (leapsum=0)
days=31;
else
days=31;}
cout<<"March"<<" "<<firstday<<" "<<year;
break;
case 4:
month;
{if (leapsum=0)
days=30;
else
days=30;}
cout<<"April"<<" "<<firstday<<" "<<year;
break;

case 5:
month;
{if (leapsum=0)
days=31;
else
days=31;}
cout<<"May"<<" "<<firstday<<" "<<year;
break;

case 6:
month;
{if (leapsum=0)
days=30;
else
days=30;}
cout<<"June"<<" "<<firstday<<" "<<year;
break;

case 7:
month;
{if (leapsum=0)
days=31;
else
days=31;}
cout<<"July"<<" "<<firstday<<" "<<year;
break;

case 8:
month;
{if (leapsum=0)
days=31;
else
days=31;}
cout<<"August"<<" "<<firstday<<" "<<year;
break;

case 9:
month;
{if (leapsum=0)
days=30;
else
days=30;}
cout<<"September"<<" "<<firstday<<" "<<year;
break;

case 10:
month;
{if (leapsum=0)
days=31;
else
days=31;}
cout<<"October"<<" "<<firstday<<" "<<year;
break;

case 11:
month;
{if (leapsum=0)
days=30;
else
days=30;}
cout<<"November"<<" "<<firstday<<" "<<year;
break;

case 12:
{if (leapsum=0)
days=31;
else
days=31;}
month;
cout<<"December"<<" "<<firstday<<" "<<year;
break;

default:
cout<<"Please restart program and enter correct month and year";
}



return 0;

}


Last edited on Oct 25, 2008 at 4:39am
Oct 25, 2008 at 4:34am
one thing I can tell you right away is that you can remove about 50 lines of code without changing anything.
in each month you have:

{if (leapsum=0)
days=30;
else
days=30;}

you could just as easily have done
days=30;
and be done with it. The only place that you even need the if statement is in February, all the other months have the same number of days even in a leap year.

and in each case you have a line that just says

month;

what is the purpose of this, it does nothing, except probably throw a compiler error.

And I may be missing something, but I don't see anywhere that the whole daysum thing is even used. What is daysum supposed to calculate, and how is that calculation supposed to be displayed?
Oct 25, 2008 at 7:04am
And there is confusion between = and the == operators.
Oct 25, 2008 at 7:29am
This is new code. Please excuse me if it seems bad. I'm brand new at this. Please help and give me pointers on how i can fix it. Also i can't get the day to appear in startday. It just prints junk.

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
void Weekday(char[10],int&);
using namespace std;
int main ()

{
int year, month, leapsum;
int days;
int firstday, fday;
char startday [10];




cout<<"Please enter the number of the month and year to access calendar\n"<<endl;

cout<< "Please leave a space in between the month and year when you enter them: \n";

cin>>month>>year;

firstday=(((year-1)*365)+4*((year-1)/4)-4*((year-1)/100)+4*((year-1)/400)+1)%7;

leapsum=(!(year%4)&&(year%100))||!(year%400);


system("cls");

Weekday(startday,fday);

switch (month)
{case 1:
month;
{if (leapsum=0)
days=31;
else
days=31;}

cout<<"January"<<" "<<startday<<" "<<year;
break;

case 2:
month;
{if (leapsum=0)
days=29;
else
days=28;}

cout<<"February"<<" "<<startday<<" "<<year;
break;

case 3:
month;
{if (leapsum=0)
days=31;
else
days=31;}

cout<<"March"<<" "<<startday<<" "<<year;
break;
case 4:
month;
{if (leapsum=0)
days=30;
else
days=30;}

cout<<"April"<<" "<<startday<<" "<<year;
break;

case 5:
month;
{if (leapsum=0)
days=31;
else
days=31;}

cout<<"May"<<" "<<startday<<" "<<year;
break;

case 6:
month;
{if (leapsum=0)
days=30;
else
days=30;}

cout<<"June"<<" "<<startday<<" "<<year;
break;

case 7:
month;
{if (leapsum=0)
days=31;
else
days=31;}

cout<<"July"<<" "<<startday<<" "<<year;
break;

case 8:
month;
{if (leapsum=0)
days=31;
else
days=31;}

cout<<"August"<<" "<<startday<<" "<<year;
break;

case 9:
month;
{if (leapsum=0)
days=30;
else
days=30;}

cout<<"September"<<" "<<startday<<" "<<year;
break;

case 10:
month;
{if (leapsum=0)
days=31;
else
days=31;}

cout<<"October"<<" "<<startday<<" "<<year;
break;

case 11:
month;
{if (leapsum=0)
days=30;
else
days=30;}

cout<<"November"<<" "<<startday<<" "<<year;
break;

case 12:
{if (leapsum=0)
days=31;
else
days=31;}
month;

cout<<"December"<<" "<<startday<<" "<<year;
break;

default:
cout<<"Please restart program and enter correct month and year";
}

fday=firstday;

return 0;

}
void Weekday(char startday[10], int& fday)
{
switch (fday)


{ case 1:
{if(fday==1)
cout<< "Monday";}
break;

case 2:
{if (fday==2)
cout<< "Tuesday";}
break;

case 3:
{if(fday==3)
cout<< "Wednesday";}
break;

case 4:
{if (fday==4)
cout<< "Thursday";}
break;

case 5:
{if (fday==5)
cout<< "Friday";}
break;

case 6:
{if (fday==6)
cout<< "Saturday";}
break;

case 7:
{if (fday==0)
cout<< "Sunday";}
break;

}




}
Oct 25, 2008 at 7:42am
The program has to calculate the month of any year and the day the month starts on. I working on the day that the month starts on. some help would be great.
Oct 25, 2008 at 11:20am
-use the #format when uploading code
-dont do the following:
1
2
3
4
5
6
7
8
if (condition)
{
...          //do someting
}
else
{
...         //do excactly the same thing
}


-your function Weekday() dont work. You only need one parameter (fday), wich you have to initialize before calling it. The function needs to pass a value to startday.
-You're treating firstday and leapsum as boolvariables: what are you trying to do with the && and || stuff? This returns either true or false, and when you initialize a integer with true or false, its value become 1(true) or 0(false)
Last edited on Oct 25, 2008 at 11:21am
Oct 25, 2008 at 11:26am
Most of your program can be thrown away
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <string>

using namespace std;
int main ()

{
    string dayNames[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    string  months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    
    int year, month, leapsum;
    int days;
    int firstday, fday;

    cout<<"Please enter the number of the month and year to access calendar\n"<<endl;

    cout<< "Please leave a space in between the month and year when you enter them: \n";

    cin>>month>>year;

    //I can see you thinking here - but it is not correct
    firstday=( ((year-1)*365) +  4*((year-1)/4)  -  4*((year-1)/100)  +  4*((year-1)/400)+1 )%7;

    leapsum=(!(year%4)&&(year%100))||!(year%400); // keep this - will be usefull later

    cout << "First day of " << months[month-1] << " of " << year << "is a " << dayNames[firstday] << endl;

    system("pause");

}



You calculation for first day of the year seems to be just a wild guess.
It seems to me that you are taking the number of days of all the completed years since
year 0000 A.D and trying to add to add on the extra days to account for
the number of leap years.

Not going to work with that formula.

I think we need to come up with a more sensible/practical method.
Last edited on Oct 25, 2008 at 12:15pm
Oct 25, 2008 at 3:53pm
firstday=( ((year-1)*365) + floor*((year-1)/4) - floor*((year-1)/100) + floor*((year-1)/400)+1 )%7;
that is the original calculation bui can't get the floor value to work?

error C2563: mismatch in formal parameter list
error C2563: mismatch in formal parameter list
error C2563: mismatch in formal parameter list

The first day calculation keeps telling me the first day of a year, i and its the same day for that perticular year.
"firstday=( ((year-1)*365) + 4*((year-1)/4) - 4*((year-1)/100) + 4*((year-1)/400)+1 )%7;"

Oct 25, 2008 at 4:29pm
This works, see if you can understand it.
PS - I limit it to starting from year 1800;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <string>

using namespace std;

 const int firstYear = 1800; //This is our start point
 const int dayOffset = 3; //The first day of our start year may not be a Sunday ( in 1800 it was Wednesday)
 const int firstLeapYear = 1804; //help to reduce how many leap years we have to check
 
 string dayNames[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
 
 string  months[] = { "January", "February", "March", "April", "May", "June", "July", "August", 
        "September", "October", "November", "December"};
 
 int daysPassed[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; //per month per year


//helper function declarations
int calcDaysSoFar( int year, int month );
bool isLeapYear( int year);


int main ()

{

    int year, month;
    int days;

    cout<<"Please enter the number of the month and year to access calendar\n"<<endl;
    cout<< "Please leave a space in between the month and year when you enter them: \n";

    cin>>month>>year;

    //Calculate how many days have passed since Jan 1 of Start year to start of requested year & month
    days = calcDaysSoFar(year, month);
    
    
    int firstDayOfWeek = days % 7;

    cout << "First day of " << months[month-1] << " " << year << " is a " << dayNames[firstDayOfWeek] << endl;

    system("pause");

}


//This function calculates the number of days passed from some start point year
int calcDaysSoFar( int year, int month)
{
    int days;

    //calculates basic number of days passed 
    days = (year - firstYear) * 365;
    days += dayOffset;
    days += daysPassed[month-1];
    
    //add on the extra leapdays for past years
    for (int count = firstLeapYear; count < year ; count +=4)
    {
        if (isLeapYear(count) )
        {
            days++;
        }
    }

    //add leapday for this year if requested  month is greater than  Feb  
    if( month > 2 && isLeapYear(year) )
    {
        days++;
    }

    return days;

}


//This function checks whether a particular year is a leap year
bool isLeapYear( int year)
{
    return (!(year%4)&&(year%100))||!(year%400);
}
Oct 25, 2008 at 4:48pm
The program works fine, so i'm going to start work on the calendar part.
it has to say the number of days in a month and put them in a chart.
Oct 25, 2008 at 8:30pm
so this is what I have , but i can't get the chart, the numbers inside the chart, and the days to show on the screen. and I don't know where to put the setw spaces.

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <string>

using namespace std;

const int firstYear = 1800;
const int dayOffset = 3;
const int firstLeapYear = 1804;

string dayNames[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

string months[] = { "January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};

int daysPassed[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};

int calcDaysSoFar( int year, int month );
bool isLeapYear( int year);
char Calendar (const char[]chart);

int main ()

{
char chart;
int year, month;
int days;

cout<<"Please enter the number of the month and year to access calendar\n"<<endl;
cout<< "Please leave a space in between the month and year when you enter them: \n";

cin>>month>>year;

days = calcDaysSoFar(year, month);


int firstDayOfWeek = days % 7;

cout << "First day of " << months[month-1] << " " << year << " is a " << dayNames[firstDayOfWeek] << endl;

Calendar (chart);
cout<<chart<<endl;

system("pause");

}



int calcDaysSoFar( int year, int month)
{
int days;


days = (year - firstYear) * 365;
days += dayOffset;
days += daysPassed[month-1];


for (int count = firstLeapYear; count < year ; count +=4)
{
if (isLeapYear(count) )
{
days++;
}
}


if( month > 2 && isLeapYear(year) )
{
days++;
}

return days;

}



bool isLeapYear( int year)
{
return (!(year%4)&&(year%100))||!(year%400);
}

char Calendar (const char[] chart)

{ Top= (char (201),char(205), char(203), char(205), char (203), char(205), char (187));
2line=(char(204),char(205),char(206),char(205), char(203),char(205), char(203),
char(205),char(203), char(205), char(203), char(205), char(206), char(205), char(185));
3line= (char(204),char (205), char(206),char (205),char(206),char (205),char(206),char (205),
char(206),char (205),char(206),char (205),char(206),char (205), char (204));
4line=(char(204),char (205), char(206),char (205),char(206),char (205),char(206),char (205),
char(206),char (205),char(206),char (205),char(206),char (205), char (204));
5line=(char(204),char (205), char(206),char (205),char(206),char (205),char(206),char (205),
char(206),char (205),char(206),char (205),char(206),char (205), char (204));
6line=(char(204),char (205), char(206),char (205),char(206),char (205),char(206),char (205),
char(206),char (205),char(206),char (205),char(206),char (205), char (204));
7line=(char(204),char (205), char(206),char (205),char(206),char (205),char(206),char (205),
char(206),char (205),char(206),char (205),char(206),char (205), char (204));
8line=(char(204),char (205), char(206),char (205),char(206),char (205),char(206),char (205),
char(206),char (205),char(206),char (205),char(206),char (205), char (204));
bottom=(char (200),char 205, char(202),char (205),char(202),char (205),char(202),char (205),
char(202),char (205),char(202),char (205),char(202),char (205), char (188));

}




Oct 26, 2008 at 2:09pm
Are you absolutely sure that the requirement is to draw all those ascii grid characters. Maybe they just want the calendar dispalyed in a "grid-like" pattern.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <string>

using namespace std;

//helper function declarations
int calcDaysSoFar( int year, int month );
bool isLeapYear( int year);
void printCalendar(int month /*1 = January*/, int year, int firstDay /* 0 = Sunday*/);

//Globals
 const int firstYear = 1800; //This is our start point
 const int dayOffset = 3; //The first day of our start year may not be a Sunday ( in 1800 it was Wednesday)
 const int firstLeapYear = 1804; //help to reduce how many leap years we have to check
 
 string dayNames[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
 
 string  months[] = { "January", "February", "March", "April", "May", "June", "July", "August", 
        "September", "October", "November", "December"};

 int daysInMonth[] = { 31,28,31,30,31,30,31,31,30,31,30,31} ; //standard year
 
 int daysPassed[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; //per month per year


int main ()

{ 

    int year, month;
    int days;

    cout<<"Please enter the number of the month and year to access calendar\n"<<endl;
    cout<< "Please leave a space in between the month and year when you enter them: \n";

    cin>>month>>year;

    //Calculate how many days have passed since Jan 1 of Start year to start of requested year & month
    days = calcDaysSoFar(year, month);
    
    
    int firstDayOfWeek = days % 7;

    cout << "First day of " << months[month-1] << " " << year << " is a " << dayNames[firstDayOfWeek] << endl;

    printCalendar(month, year, firstDayOfWeek);

    system("pause");

}


//This function calculates the number of days passed from some start point year
int calcDaysSoFar( int year, int month)
{
    int days;

    //calculates basic number of days passed 
    days = (year - firstYear) * 365;
    days += dayOffset;
    days += daysPassed[month-1];
    
    //add on the extra leapdays for past years
    for (int count = firstLeapYear; count < year ; count +=4)
    {
        if (isLeapYear(count) )
        {
            days++;
        }
    }

    //add leapday for this year if requested  month is greater than  Feb  
    if( month > 2 && isLeapYear(year) )
    {
        days++;
    }

    return days;

}


//This function checks whether a particular year is a leap year
bool isLeapYear( int year)
{
    return (!(year%4)&&(year%100))||!(year%400);
}


//Prints Month and Year
//followed by the day headings
//followed by the numbers
void printCalendar(int month, int year, int firstDay)
{
    cout << '\n';
        
    cout << months[month-1] << "     " << year << '\n'; 
    cout << "Sun " << "Mon " << "Tue " << "Wed " << "Thu " << "Fri " << "Sat "<< '\n';

    int count,offset;

    offset= 1 - firstDay;
    
    count = daysInMonth[month-1]; //get the number of days in this month

    //adjust if dealing with February and year is leap year
    if(isLeapYear(year) && (month==2) )
    {
        count++;
    }


    for (int x = offset; x <= count; x +=7)
    {

        for(int column= x; (column < x+7) && (column <= count); column++)
        {
            if (column <=0)
            {
                cout << setw(4) << left << setprecision(3) << " "; 
            }
            else
            {
                cout << setw(4) << left << setprecision(3) << setfill(' ') << column;
            }

        }

        cout << '\n';
    }

    cout << endl;

}
Oct 26, 2008 at 2:35pm
Also, to view an entire year's calendar (slightly modified from guestgulkan):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <string>

using namespace std;

//helper function declarations
int calcDaysSoFar( int year, int month );
bool isLeapYear( int year);
void printCalendar(int month /*1 = January*/, int year, int firstDay /* 0 = Sunday*/);

//Globals
 const int firstYear = 1800; //This is our start point
 const int dayOffset = 3; //The first day of our start year may not be a Sunday ( in 1800 it was Wednesday)
 const int firstLeapYear = 1804; //help to reduce how many leap years we have to check
 
 string dayNames[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
 
 string  months[] = { "January", "February", "March", "April", "May", "June", "July", "August", 
        "September", "October", "November", "December"};

 int daysInMonth[] = { 31,28,31,30,31,30,31,31,30,31,30,31} ; //standard year
 
 int daysPassed[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; //per month per year


int main ()

{ 

    int year, month = 0;
    int days;
    cout << "Enter year to view calendar: ";
    cin >> year;
    do{
    //Calculate how many days have passed since Jan 1 of Start year to start of requested year & month
    days = calcDaysSoFar(year, month);
    
    
    int firstDayOfWeek = days % 7;

    printCalendar(month, year, firstDayOfWeek);
    month++;
    }while(month < 12);
    system("pause");

}


//This function calculates the number of days passed from some start point year
int calcDaysSoFar( int year, int month)
{
    int days;

    //calculates basic number of days passed 
    days = (year - firstYear) * 365;
    days += dayOffset;
    days += daysPassed[month-1];
    
    //add on the extra leapdays for past years
    for (int count = firstLeapYear; count < year ; count +=4)
    {
        if (isLeapYear(count) )
        {
            days++;
        }
    }

    //add leapday for this year if requested  month is greater than  Feb  
    if( month > 2 && isLeapYear(year) )
    {
        days++;
    }

    return days;

}


//This function checks whether a particular year is a leap year
bool isLeapYear( int year)
{
    return (!(year%4)&&(year%100))||!(year%400);
}


//Prints Month and Year
//followed by the day headings
//followed by the numbers
void printCalendar(int month, int year, int firstDay)
{
    cout << months[month-1] << "\t" << year << '\n'; 
    cout << "Sun " << "Mon " << "Tue " << "Wed " << "Thu " << "Fri " << "Sat "<< '\n';

    int count,offset;

    offset= 1 - firstDay;
    
    count = daysInMonth[month-1]; //get the number of days in this month

    //adjust if dealing with February and year is leap year
    if(isLeapYear(year) && (month==2) )
    {
        count++;
    }


    for (int x = offset; x <= count; x +=7)
    {

        for(int column= x; (column < x+7) && (column <= count); column++)
        {
            if (column <=0)
            {
                cout << setw(4) << left << setprecision(3) << " "; 
            }
            else
            {
                cout << setw(4) << left << setprecision(3) << setfill(' ') << column;
            }

        }

        cout << '\n';
    }

    cout << endl;

}
Last edited on Oct 26, 2008 at 2:36pm
Oct 26, 2008 at 6:23pm
The char table has to be used with the calendar. and it ahas to connect in this order. { Top= (char (201),char(205), char(203), char(205), char (203), char(205), char (187));
2line=(char(204),char(205),char(206),char(205), char(203),char(205), char(203),
char(205),char(203), char(205), char(203), char(205), char(206), char(205), char(185));
3line= (char(204),char (205), char(206),char (205),char(206),char (205),char(206),char (205),
char(206),char (205),char(206),char (205),char(206),char (205), char (204));
4line=(char(204),char (205), char(206),char (205),char(206),char (205),char(206),char (205),
char(206),char (205),char(206),char (205),char(206),char (205), char (204));
5line=(char(204),char (205), char(206),char (205),char(206),char (205),char(206),char (205),
char(206),char (205),char(206),char (205),char(206),char (205), char (204));
6line=(char(204),char (205), char(206),char (205),char(206),char (205),char(206),char (205),
char(206),char (205),char(206),char (205),char(206),char (205), char (204));
7line=(char(204),char (205), char(206),char (205),char(206),char (205),char(206),char (205),
char(206),char (205),char(206),char (205),char(206),char (205), char (204));
8line=(char(204),char (205), char(206),char (205),char(206),char (205),char(206),char (205),
char(206),char (205),char(206),char (205),char(206),char (205), char (204));
bottom=(char (200),char 205, char(202),char (205),char(202),char (205),char(202),char (205),
char(202),char (205),char(202),char (205),char(202),char (205), char (188));

}
Oct 27, 2008 at 2:02am
Can someone tell me me how to insert them in the program?
Oct 27, 2008 at 11:58am
If this is a course assignment, I hope you give credit where it is due and acknowledge the help from the cplusplus forum.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <string>
#include <sstream>


using namespace std;

//helper function declarations
int calcDaysSoFar( int year, int month );
bool isLeapYear( int year);
void printCalendar(int month /*1 = January*/, int year, int firstDay /* 0 = Sunday*/);
void printGridTop(int innerWidth);
void repeatChar(char ch, int numChar);
void drawDivider(int innerWidth);
void printGridBottom(int innerWidth);


//Globals
 const int firstYear = 1800; //This is our start point
 const int dayOffset = 3; //The first day of our start year may not be a Sunday ( in 1800 it was Wednesday)
 const int firstLeapYear = 1804; //help to reduce how many leap years we have to check
 
 string dayNames[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
 
 string  months[] = { "January", "February", "March", "April", "May", "June", "July", "August", 
        "September", "October", "November", "December"};

 int daysInMonth[] = { 31,28,31,30,31,30,31,31,30,31,30,31} ; //standard year
 
 int daysPassed[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; //per month per year


int main ()

{ 

    int year, month;
    int days;

    cout<<"Please enter the number of the month and year to access calendar\n"<<endl;
    cout<< "Please leave a space in between the month and year when you enter them: \n";

    cin>>month>>year;

    //do{
    //Calculate how many days have passed since Jan 1 of Start year to start of requested year & month
    days = calcDaysSoFar(year, month);
    int firstDayOfWeek = days % 7;
    
    printCalendar(month, year, firstDayOfWeek);
    //month++;
    //}while(month < 13); //need to be 13 because months are numbered 1 - 12
       
    system("pause");

}


//This function calculates the number of days passed from some start point year
int calcDaysSoFar( int year, int month)
{
    int days;

    //calculates basic number of days passed 
    days = (year - firstYear) * 365;
    days += dayOffset;
    days += daysPassed[month-1];
    
    //add on the extra leapdays for past years
    for (int count = firstLeapYear; count < year ; count +=4)
    {
        if (isLeapYear(count) )
        {
            days++;
        }
    }

    //add leapday for this year if requested  month is greater than  Feb  
    if( month > 2 && isLeapYear(year) )
    {
        days++;
    }

    return days;

}


//This function checks whether a particular year is a leap year
bool isLeapYear( int year)
{
    return (!(year%4)&&(year%100))||!(year%400);
}


//Prints Month and Year
//followed by the day headings
//followed by the numbers
void printCalendar(int month, int year, int firstDay)
{
    string dayHeader = string("Sun ") + char(186) + "Mon " + char(186) + "Tue " + char(186) +
        "Wed " + char(186) + "Thu " + char(186) + "Fri " + char(186) + "Sat ";
    
    int innerWidth = dayHeader.length();
    stringstream sstream; //for converting the year to a string.
    string tempStr;

    cout << '\n';
    printGridTop(innerWidth);
    
    //print the month/year line
    sstream << year;
    tempStr = string(" ") + months[month-1] + string("     ") + sstream.str();
    cout << char(186);
    cout << tempStr;
    repeatChar(' ', innerWidth - tempStr.length());
    cout << char (186);
    
    //Draw a dividing line
    cout << '\n';
    cout << char(204);
    for (int count =0; count < 7; count ++)
    {
        repeatChar(char(205),4);
        if( count != 6)
        {
            cout << char(203);
        }
    }
    cout << char(185);
    cout << '\n';
    
    
    //print the day header line
    cout << char(186);
    cout << dayHeader;
    //cout << "Sun " << "Mon " << "Tue " << "Wed " << "Thu " << "Fri " << "Sat ";//<< '\n';
    cout << char(186) << '\n';
    
    drawDivider(innerWidth); //draw a dividing horizontal line
    
    int count,offset;
    offset= 1 - firstDay;
    
    count = daysInMonth[month-1]; //get the number of days in this month

    //adjust if dealing with February and year is leap year
    if(isLeapYear(year) && (month==2) )
    {
        count++;
    }

    //loop to fill in rows
    int dayNum;
    for (int x = offset; x <= count; x +=7)
    {

        cout << char(186);
        for(dayNum= x; (dayNum < x+7) /*&& (column <= count)*/; dayNum++)
        {
            if ( dayNum <=0  || dayNum > count)
            {
                cout << setw(4) << left << setprecision(3) << "    " << char(186);; 
            }
            else
            {
                cout << setw(4) << left << setprecision(3) << setfill(' ') << dayNum << char(186);
            }

        }
        cout << '\n';
        if(dayNum <= count)
        {
            //draw a divider line after each row except the last
            drawDivider(innerWidth);
        }
    }

    printGridBottom(innerWidth);

}


//draws the top of the drid
void printGridTop(int innerWidth)
{
    cout << char (201) ;
    repeatChar((char)205,innerWidth);    
    cout << char (187);
    cout << '\n';

}

//this just repeats a char a nomber of times
void repeatChar(char ch, int numChar)
{
    string aStr(numChar, ch);
    cout << aStr;
}

//draws a line between rows
void drawDivider(int innerWidth)
{
    cout << char(204);
    for (int count =0; count < 7; count ++)
    {
        repeatChar(char(205),4);
        if( count != 6)
        {
            cout << char(206);
        }
    }
    cout << char(185);
    cout << '\n';
}

//draws the bottom line of the grid
void printGridBottom(int innerWidth)
{
    cout << char(200);
    for (int count =0; count < 7; count ++)
    {
        repeatChar(char(205),4);
        if( count != 6)
        {
            cout << char(202);
        }
    }
    cout << char(188);
    cout << '\n';
}



If you want the loop back in then uncomment lines 48, 54 and 55.
The loop will print the calendar from the requested month up to the end of the year.
So if user enters - 4 2008 - it will print April up to December 2008.

Note that you should put in some checks to validate the month and year values entered by the user.
Last edited on Oct 27, 2008 at 12:33pm
Oct 27, 2008 at 3:00pm
I will thank you very much
Topic archived. No new replies allowed.