0% found this document useful (0 votes)
102 views

C++ Program

This C++ program defines a class called mydate that represents a date with day, month and year attributes. The mydate class contains methods to read in a date from the user, validate if the date exceeds the number of days in the given month, convert the numeric month to a string, check if a year is a leap year, and calculate the date for the next day. The main function creates a mydate object, reads in a date from the user, and calls the method to calculate tomorrow's date.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

C++ Program

This C++ program defines a class called mydate that represents a date with day, month and year attributes. The mydate class contains methods to read in a date from the user, validate if the date exceeds the number of days in the given month, convert the numeric month to a string, check if a year is a leap year, and calculate the date for the next day. The main function creates a mydate object, reads in a date from the user, and calls the method to calculate tomorrow's date.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

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.

// Input date from user

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.

return "Unknown month";


}
// return TRUE if a year is leap
int mydate::isLeapYear(){
if ( (year % 4)

!= 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() ){

cerr << year << " is not a leap year, so Feb


doesn't have 29 days" << endl;

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; }
}

cout << "Valid date, found and tommorow is " <<


<< tm << "/" << ty << endl;
100.
}
101.
102.
// main

103.
104.

int main(){
mydate date;

105.
106.
107.
108.
109.

date.readdate();
date.tommorow();
return 0;
}

td << "/"

You might also like