C++ Program
C++ Program
2.
3.
4.
5.
6.
7.
#include<iostream>
8.
9.
10.
11.
class mydate{
12.
#define TRUE 1
#define FALSE 0
using namespace std;
int day;
int month;
int year;
public:
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.
void readdate();
int isLeapYear();
string month2string();
int exceedsDaysInMonth();
void tommorow();
void mydate::readdate(){
cout << "Enter day (dd - 23 ) : ";
cin >> day;
cout << "Enter Month (mm - 04 ) : ";
cin >> month;
cout << "Enter year (yyyy - 2007 ) : ";
cin >> year;
}
// make sure month days are correct
// return TRUE if days exceeds in a month
int mydate::exceedsDaysInMonth(){
int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
if ( month < 1 || month > 12 || day > days[month-1])
return TRUE;
else
return FALSE;
}
// convert numeric month into string
string mydate::month2string(){
char *months[] =
{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","
Dec"};
41.
42.
43.
44.
45.
46.
47.
if ( exceedsDaysInMonth() ) {
if ( ! (month < 1 || month > 12) )
return months[month-1];
else
return "Unknown month";
}
else
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.
!= 0 ){
return FALSE;
}
else if ( (year % 400)
!= 0 ){
return TRUE;
}
else if ( (year % 100)
== 0 ){
return FALSE;
}
else
{
return FALSE;
}
}
// validate and calculate tommorows date
void mydate::tommorow(){
int td, tm, ty;
int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
if ( year < 0 ){
cerr << year << " is not a valid year" << endl;
exit(1);
}
if ( exceedsDaysInMonth() ){
if ( month == 2 && day == 29 ){
if ( ! isLeapYear() ){
78.
79.
80.
81.
82.
83.
exit(1);
}
}
else
{
cerr << "Bad day value month - " << month << " (" <<
month2string();
84.
85.
86.
87.
88.
89.
90.
91.
cerr <<
") doesn't have " << day << " days "<< endl;
exit(1);
}
}
// calculate tommorow
td=day;
tm=month;
ty=year;
92.
td++;
93.
94.
if ( td > days[month-1]){
td=1;
tm++;
95.
96.
97.
98.
99.
if ( tm > 12 )
{ ty++; tm=1; }
}
103.
104.
int main(){
mydate date;
105.
106.
107.
108.
109.
date.readdate();
date.tommorow();
return 0;
}
td << "/"