Need help with my class functions

Hello all,

I have a homework assignment doing some weird things, I'm genuinely not sure why. I took the previous lab assignment, and have been building off that instead of writing this program from scratch.

Heres my main function file:

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
#include <iostream>
#include "dateType.h"

using namespace std;

int main()
{

	dateType d1;
	int month = 0, day = 0, year = 0, monthDays = 0;

	cout << "Enter the date" << endl;
	cout << "Enter the numeric moth: ";
	cin >> month;
	cout << endl;

	cout << "Enter the Numeric Day: ";
	cin >> day;
	cout << endl;

	cout << "Enter the Year: ";
	cin >> year;
	cout << endl;

	d1.setDate(month, day, year);

	d1.printDate();

	d1.dayspassed();

	d1.daysleft();

	system("pause");
	return 0;
}


Before "Enter the date" appears on the screen, there is output from another function. But how can that be, when I didn't call a function?

Heres my other .cpp file:

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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
 
#include <iostream>
#include "dateType.h"

using namespace std;

void dateType::setDate(int month, int day, int year)
{
	int maxDays = 0;
		
	if (year > 1699 && year < 4000)
	{
		dYear = year;
	}
	else
		dYear = 2014;
		

	if (month > 0 && month < 13)
	{
		dMonth = month;
	switch (month)
	{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
		maxDays = 31;
		break;
	case 4:
	case 6:
	case 9:
	case 11:
		maxDays = 30;
		break;
	case 2:
	{
			  if (isLeapYear(year) == true)
				  maxDays = 29;
			  else
				  maxDays = 28;
			  break;
	}
	default:
		dDay = 31;
	}
	}


	if (day < 1 || day > maxDays)
		dDay = 1;
	else
		dDay = day;

	cout << "The Maximum days in the month are: " << maxDays << endl;
 

}

int dateType::getDay() const 
{
    return dDay;
}

int dateType::getMonth() const 
{
    return dMonth;
}

int dateType::getYear() const 
{
    return dYear;
}

bool dateType::isLeapYear(int year)
{
	if (year % 4 != 0) {
		return false;
	}
	else if (year % 400 == 0) {
		return true;
	}
	else if (year % 100 == 0) {
		return false;
	}
	else {
		return true;
	}
}

void dateType::printDate() const
{
	cout << "The date you entered is:" << endl;
    cout << dMonth << "/" << dDay << "/" << dYear << endl;
}

    
dateType::dateType(int month, int day, int year) 
{ 
	setDate(month, day, year);
}

void dateType::dayspassed()
{
	//PEMDAS
	int maxDays = 0, maximumDays = 365, jan = 31, feb = 28, mar = 31, apr = 30, may = 31, jun = 30, jul = 31, aug = 31, sept = 30, oct = 31, nov = 30, dec = 31;

	int month = dMonth, year = dYear, days = dDay, passed = 0;

	if (isLeapYear(year) == true)
	{  feb = 29;
	}
	 else
	{	 feb = 28;
	}

	switch (month)
	{
	case 1:
		{
			passed = days;
		}
	case 2:
	{  
			passed = jan + days;
	}
	case 3:
		{
			passed = jan + feb + days;
		}
	case 4:
		{
			passed = jan + feb + mar + days;
		}
	case 5:
		{
			passed = jan + feb + mar + apr + days;
		}
	case 6:
		{
			passed = jan + feb + mar + apr + may + days;
		}
	case 7:
		{
			passed = jan + feb + mar + apr + may + jun + days;
		}
	case 8:
		{
			passed = jan + feb + mar + apr + may + jun + jul + days;
		}
	case 9:
		{
			passed = jan + feb + mar + apr + may + jun + jul + aug + days;
		}
	case 10:
		{
			passed = jan + feb + mar + apr + may + jun + jul + aug + sept + days;
		}
	case 11:
		{
			passed = jan + feb + mar + apr + may + jun + jul + aug + sept + oct + days;
		}
	case 12:
		{
			passed = jan + feb + mar + apr + may + jun + jul + aug + sept + oct + nov + days;
		}

	default:
		month = 0;
	}

	cout << passed << " Days have passed" << endl;

}

void dateType::daysleft()
{
	
	int maxDays = 0, maximumDays = 365, jan = 31, feb = 28, mar = 31, apr = 30, may = 31, jun = 30, jul = 31, aug = 31, sept = 30, oct = 31, nov = 30, dec = 31;

	int month = dMonth, year = dYear, days = dDay, left = 0;

	if (isLeapYear(year) == true)
	{
		feb = 29;
	}
	else
	{
		feb = 28;
	}

	switch (month)
	{
	case 1:
	{
			  left = jan + feb + mar + apr + may + jun + jul + aug + sept + oct + nov + dec + days;
	}
	case 2:
	{
			  left = feb + mar + apr + may + jun + jul + aug + sept + oct + nov + dec + days;
	}
	case 3:
	{
			  left = mar + apr + may + jun + jul + aug + sept + oct + nov + dec + days;
	}
	case 4:
	{
			  left = apr + may + jun + jul + aug + sept + oct + nov + dec + days;
	}
	case 5:
	{
			  left = may + jun + jul + aug + sept + oct + nov + dec + days;
	}
	case 6:
	{
			  left = jun + jul + aug + sept + oct + nov + dec + days;
	}
	case 7:
	{
			  left = jul + aug + sept + oct + nov + dec + days;
	}
	case 8:
	{
			  left = aug + sept + oct + nov + dec + days;
	}
	case 9:
	{
			  left = sept + oct + nov + dec + days;
	}
	case 10:
	{
			   left = oct + nov + dec + days;
	}
	case 11:
	{
			   left = nov + dec + days;
	}
	case 12:
	{
			   left = dec + days;
	}

	default:
		month = 0;
	}

	cout << "There are " << left << " Days Left" << endl;
}


Last edited on
dateType d1;

Here, the default constructor gets called.

101
102
103
104
dateType::dateType(int month, int day, int year) 
{ 
	setDate(month, day, year);
}
Your constructor calls setDate, which has this line:
58
59
60
61
	cout << "The Maximum days in the month are: " << maxDays << endl;
 

}
oh thanks you so much!

I wish I had gotten to write this thing from scratch. In the lab we were handed 90% of this thing, then told to make it check to see if the date entered is a leap year or not.

I'm too afraid to change anything in it because I'm still not 100% on how it all works.

Now I have to make a program that checks the year to see if its a leap year, add up the days that have passed up to the entered date, then add up the days that have yet to pass. I'm re-using this program because it had most of what I needed.

Is there a problem with my math on the days passed and days left functions?

passed is supposed to add up the days that have passed up to the date entered by the user, and left is supposed to add up the days that are left in the year.
I don't see where you're accounting for the extra day in February on leap years.
It should be calculating it here:

1
2
3
4
5
6
7
8
if (isLeapYear(year) == true)
	{
		feb = 29;
	}
	else
	{
		feb = 28;
	}


oh the isleapyear function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool dateType::isLeapYear(int year)
{
	if (year % 4 != 0) {
		return false;
	}
	else if (year % 400 == 0) {
		return true;
	}
	else if (year % 100 == 0) {
		return false;
	}
	else {
		return true;
	}
}


I ask because I'm getting some weird outputs.

Enter the date
Enter the numeric moth: 7

Enter the Numeric Day: 2

Enter the Year: 1998

The date you entered is:
7/2/1998
336 Day(s) have passed
There are 29 Days Left

This is what I see after I run it.

The output date works fine, but figuring out how many days have passed, and how many are left seems to be the issue.
bump
Your switch cases require break statements, at least as written. You could take advantage of the way drop-through works and rewrite the switch portion to look like so:

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
void dateType::dayspassed()
{
    //PEMDAS
    int maxDays = 0, maximumDays = 365, jan = 31, feb = 28, mar = 31, apr = 30, may = 31, jun = 30, jul = 31, aug = 31, sept = 30, oct = 31, nov = 30, dec = 31;

    int month = dMonth, year = dYear, days = dDay, passed = 0;

    feb = isLeapYear(year) ? 29 : 28 ;

    switch (month)
    {
    case 12: passed += nov ;
    case 11: passed += oct ;
    case 10: passed += sept ;
    case 9: passed += aug ;
    case 8: passed += jul ;
    case 7: passed += jun ;
    case 6: passed += may ;
    case 5: passed += apr ;
    case 4: passed += mar ;
    case 3: passed += feb ;
    case 2: passed += jan ;
    case 1: passed += days ;
    }

	cout << passed << " Days have passed" << endl;
}

Last edited on
wait, so all I need to add is a break statement to each one of those switch cases to get them to run as intended?
You could add break statements to your code as-is, or you could do it the easier way that cire suggested.
Topic archived. No new replies allowed.