What's wrong with my code?

Please help me fix my Code! Here's the problem question:
Q. Create a class Date that has two integer members day and month. Create a default constructor and a parameterized constructor. Create three member functions getDate( ), showDate( ) and addDate( ). The addDate( ) function should adds two dates. [Do not include year in the program. If month exceeds 12 then it should display the message “New Year”

I think my code is wrong at the addDate() function's loops

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
 #include<iostream.h>
#include<conio.h>
int maxdays(int m)
{
int dmax;
switch(m)
	{
	case 1:{dmax=31;break;}
	case 2:{dmax=28;break;}
	case 3:{dmax=31;break;}
	case 4:{dmax=30;break;}
	case 5:{dmax=31;break;}
	case 6:{dmax=30;break;}
	case 7:{dmax=31;break;}
	case 8:{dmax=31;break;}
	case 9:{dmax=30;break;}
	case 10:{dmax=31;break;}
	case 11:{dmax=30;break;}
	case 12:{dmax=31;break;}
	}
return dmax;
}
class Date
{
private:
	int day,month;
public:
	int dayscalc(Date dt)
	{
	int d,m,t,count=0;
	d=dt.day;
	m=dt.month;
	for(int i=1;i<=m;i++)
		{
		 if(i==m)
		 t=d;
		 else
		 t=maxdays(m);
		 for(int i=1;i<=t;i++)
			{
			count++;
			}
		 }
	return count;
	}

	void getDate()
	{
	cout<<"\nEnter Day (numeric):";
	cin>>day;
	cout<<"\nEnter Month (numeric):";
	cin>>month;
	}
	void showDate()
	{
	cout<<"\nThe Day is:"<<day<<" and the month is:"<<month;
	}
	void addDate(Date d1, Date d2)
	{
	int a=dayscalc(d1),b=dayscalc(d2),c=a+b,d,e,flag=0;
	int dys=1,mts=1;
	if(c>365)
	       {
	       c-=365;
	       cout<<"\nNew Year";
	       }
	for(int i=1;i<=c;i++)
		{
		e=0,d=0;
		 for(int j=1;j<=12;j++)
			{
			if (flag==1)
			break;
			e=maxdays(j);
			d+=e;
			if(i>d)
			mts++;
			else if((i==c)&&(d==i))
				{
				d-=e;
				dys=c-d;
				flag=1;
				day=dys;
				month=mts;
				break;
				}
			}
		}

	}
};

void main()
{
clrscr();
Date D1,D2,D3;
D1.getDate();
D2.getDate();
D3.addDate(D1,D2);
D3.showDate();
getch();
}
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
#include <iostream>
#include <cstdlib>

using namespace std;

class Date
{
public:
    Date()  //default constructor
    {}
    Date(int d, int m)  //paramterized constructor
    {
        //you should check for day and month validity with preferably exception handling
        day = d;
        month = m;
    }
    void showDate()
    {
        cout<<"Date: "<<day<<"/"<<month<<endl;
    }
    Date *getDate()
    {
        return this;    //return pointer to the object
    }
    void addDate(Date *date1, Date *date2)
    {
        Date newDate(date1->day + date2->day,date1->month + date2->month);  //copy constructor
        if(newDate.month > 12)
            cout<<"NEW YEAR"<<endl;
        newDate.showDate();
    }
private:
    int day;
    int month;
};
int main()
{
    Date date1(4,7);
    Date date2(3,9);
    Date date3;

    date1.showDate();
    date2.showDate();
    date3.addDate(date1.getDate(),date2.getDate());
    
   system("pause");
    return 0;
}
Topic archived. No new replies allowed.