How to print first and last day of month Only

Hello all,

I am new to the forums so let me know if this code is not beginner level and I will post it somewhere else.

I am also new to programming, so I am struggling a bit with this code.
I need to make a code to print the first day of the month and last, for the year and month the user inputs.

example
cout "please type a month and year "
if user types 04 2013

The code I write has to print

1 monday
30 tuesday

Only!

I can make a code to print first day of the month, or all days of the month, What I am struggling with is printing both First and Last. So If anyone experienced enough that can give me a tip on how to print Last day of the month that would be really helpful.

Reason for this is because, I have to make a GUI calender and other dates are irrelevant with my GUI code.


Below is a code exactly similar to what I wrote mine like, I will post mine later( very similar) when I am on my desktop.


---edit: P.S new to typing in English too apparently Awkward "I" sentence starter.
xD

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

#include "std_lib_facilities_3.h"



bool LeapYear (int year)

// "year" must be between 1900 and 2999.
// Returns true if "year" is a leap year.

{
// true, if year is divisible by 4, and ...
// ... either not divisible by 100, or divisible by 400. 

return (year % 4 == 0 &&
(year % 100 != 0 || year % 400 == 0));
}

//**************************************

const int daysInMonth[12] = {

31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31
};

// Days from the beginning of the year
// to the beginning of the month:

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

const char *dayName[7] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"   // ** made constant " #"
};

const int DAY0 = 1; // Day of week for 01-01-1900 is Monday = 1.

//**************************************

bool ValidDate (int month, int day, int year)

// Returns true if month-day-year is a valid date between
//   01-01-1900 and 12-31-2999.

{
    bool valid = false;  // First assume the date is invalid.
    int days;

    // If year and month have valid values:
    if (year >= 1900 && year <= 2999 &&
                                month >= 1 && month <= 12) {

        // Get the number of days in this month from the table:
        days = daysInMonth[month-1]; // (-1, because the indices
                                     //   of an array start from 0.)

        // If February of a leap year -- increment the number
        //   of days in this month:
        if (month == 2 && LeapYear(year))
            days++;

        // Check that the given day is within the range.
        //   If so, set valid to true:
        if (day >= 1 && day <= days)
            valid = true;
    }

    return valid;
}


//**************************************

long DaysSince1900 (int month, int day, int year)

// Returns the number of days elapsed since 01-01-1900
// to month-day-year

{
long days;

// Calculate days to 01-01 of this year with correction for
// all the previous leap years:
if (year == 1900)
days = 0;
else
days = long(year - 1900) * 365
+ (year - 1901) / 4 // +1 for each 4th year
- (year - 1901) / 100 // -1 for each 100th year
+ (year - 1601) / 400; // +1 for each 400th year,
// starting at 2000

// Add days for previous months with correction for
// the current leap year:
days += daysToMonth[month-1];
if (LeapYear(year) && month > 2) days++;

// Add days since the beginning of the month:
days += day - 1;

return days;
}

//**************************************

int DayOfWeek (int month, int day, int year)

// Returns the day of the week for a given date:
// 0 -- Sunday, 1 -- Monday, etc.

{
return int((DAY0 + DaysSince1900(month, day, year)) % 7);
}

int main()



{


int month, day = 1, year, days, j;
int weekday;
cout << "Please enter a month and a year==> ";
cin >> month >> year;

if (!ValidDate(month, day, year)) {
cout << "*** Invalid date ***\n";
return 1;
}

if(month == 1) days = 31;
if(month == 2) days = 28;
if(month == 3) days = 31;
if(month == 4) days = 30;
if(month == 5) days = 31;
if(month == 6) days = 30;
if(month == 7) days = 31;
if(month == 8) days = 31;
if(month == 9) days = 30;
if(month == 10) days = 31;
if(month == 11) days = 30;
if(month == 12) days = 31;

for(j = 1; j <= days; j++)
{
cout << j << " ";

weekday = DayOfWeek(month, j, year);
if(weekday == 0) cout << "Sunday 0 ";
if(weekday == 1) cout << "Monday 1 ";
if(weekday == 2) cout << "Tuesday 2 ";
if(weekday == 3) cout << "Wednesday 3 ";
if(weekday == 4) cout << "Thursday 4 ";
if(weekday == 5) cout << "Friday 5 ";
if(weekday == 6) cout << "Saturday 6";

cout << endl;
}
keep_window_open("~");	// For some Windows(tm) setups
return 0;
}
	
	
It is simple:
Instead of your loop you should output day of week for
DayOfWeek(month, 1, year) and DayOfWeek(month, days, year)
Hey thanks for the respond. :), and thanks for making it so clear!
" DayOfWeek(month, days, year)"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{


weekday = DayOfWeek(month, 1, year);
if(weekday == 0) cout << "Sunday 0 ";
if(weekday == 1) cout << "Monday 1 ";
if(weekday == 2) cout << "Tuesday 2 ";
if(weekday == 3) cout << "Wednesday 3 ";
if(weekday == 4) cout << "Thursday 4 ";
if(weekday == 5) cout << "Friday 5 ";
if(weekday == 6) cout << "Saturday 6";

cout << endl;
}


works prints first day of code.

1
2
3
4
5
6
7
8
9
10
 weekday = DayOfWeek(month, days, year);
if(weekday == 0) cout << "Sunday 0 ";
if(weekday == 1) cout << "Monday 1 ";
if(weekday == 2) cout << "Tuesday 2 ";
if(weekday == 3) cout << "Wednesday 3 ";
if(weekday == 4) cout << "Thursday 4 ";
if(weekday == 5) cout << "Friday 5 ";
if(weekday == 6) cout << "Saturday 6";

cout << endl;


Does not work? any idea what I am doing wrong?

*** Edit : It works, but what it lacks now it won't tell me what the last day is if its (28, 29, 30 or 31). Any idea how to fix that since i removed for loop with j?


2nd Edit: Nvm :) it works. Thanks
Last edited on
Topic archived. No new replies allowed.