Adding 90 Days to a Date

So I have a program that is supposed to have a user input a date, calculate tomorrow's date, yesterday's date and a day 90 days from then. I have it all down and I know it is along the lines of my tomorrow(); function but I cannot get the 90 day to work.

I'm guessing my question is how do I change my tomorrow(); function, instead of adding 1 day, to add 90 days?
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
  void Date::tomorrow()
{
	{
	int m = month, d = day, y = year;

	switch(m)


	{case 1: case 3: case 5: case 7: case 8: case 10: case 12:
		if(d < 30)
		{d ++; break;}

		else if((d <= 31) && (m < 11))
		{m ++;d = 1;break;}

		else if((d <= 31) && (m >= 12))
		{y ++;m = 1;d = 1;break;}


	case 4: case 6: case 9: case 11:
		if(d < 29)
		{d ++;break;}

		else if(d <= 30)
		{m ++;d = 1;break;
		}


	case 2: //February
		if((leapyear(y)) && (d < 29))
		{d ++;break;}

		else if((!(leapyear(y))) && (d < 28))
		{d ++;break;}

		else if((leapyear(y)) && (d >= 29))
		{m ++;d = 1;break;}

		else if ((!(leapyear(y))) && (d >= 28))
		{m ++; d = 1; break;}


	}
	cout << m << "-" << d << "-" << y << endl;
	}
}
Nobody?
What if you added one day, ninety times? This would require improving the function so that you actually keep hold of the "tomorrow" date, so you can add to it another 89 times.

This is part of the benefit of having functions. So you can call them more than once.
Last edited on
That is what I was thinking would work, but I just don't know how to implement. Can you give me any pseudo code?
month, day and year appear to be member variables of the class. Work directly on them. They will stay changed.
Last edited on
So add 89 to my month, day and year?

int m = month+89, d = day+89, y = year+89;

Is that what you're saying? I'm relatively new to programming, sorry if I sound dumb
Long time ago I wrote a date class. Have a look maybe it will work for you.
DateUtils.h:
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
#ifndef DATE_UTILS_H
#define DATE_UTILS_H

#include <string>

using std::string;

namespace DateUtils
{
  class CDate
  {
  public:
    CDate();
    CDate(int day, int month, int year);
    
    static bool IsLeapYear(int year);
    bool IsLeapYear();
    int DaysInMonth();
    int DayOfYear();
    
    int GetDay() { return m_Day;}
    int GetMonth() {return m_Month;}
    int GetYear() { return m_Year;}
    
		int DaysBetween(const CDate& other);
		void NextDay();
		void PreviousDay();
		void SubtractDays(unsigned int num_days);
		void AddDays(unsigned int);
    bool operator==(CDate& other);
    bool operator!=(CDate& other);
		bool operator<(CDate& other);

		string ToString();
  private:
    int m_Day,
        m_Month,
        m_Year;
  };
}
#endif

DateUtils.cpp
[code]
#include "Dateutils.h"
#include <windows.h>
#include <ctime>

namespace DateUtils
{
	CDate::CDate()
	{
		SYSTEMTIME  st;

		::GetSystemTime(&st);
		m_Day = (int)st.wDay;
		m_Month = (int)st.wMonth;
		m_Year = (int)st.wYear;
	}

	CDate::CDate(int day, int month, int year)
	{
		m_Day = day;
		m_Month = month;
		m_Year = year;
	}

	int CDate::DayOfYear()
	{
		static int MonthDays[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};

		return MonthDays[m_Month-1] + m_Day + ((m_Month > 2) && IsLeapYear());
	}

	int CDate::DaysInMonth()
	{
		int days[] = {-1, 31,28,31,30,31,30,31,31,30,31,30,31};

		if (IsLeapYear())
		{
			days[2] = 29;
		}

		return days[m_Month];
	}

	bool CDate::IsLeapYear()
	{
		return CDate::IsLeapYear(m_Year);
	}

	bool CDate::IsLeapYear(int year)
	{
		return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
	}

	int CDate::DaysBetween(const CDate& other)
	{
		const int seconds_per_day = 60*60*24;

		tm date1 = {0}, date2 = {0};
		time_t t1, t2;

		date1.tm_year = m_Year - 1900;
		date1.tm_mon= m_Month-1;
		date1.tm_mday = m_Day;

		date2.tm_year = other.m_Year - 1900;
		date2.tm_mon= other.m_Month-1;
		date2.tm_mday = other.m_Day;

		t1 = mktime(&date1);
		t2 = mktime(&date2);

		return (int)(difftime(t2, t1) / seconds_per_day);
	}

	void CDate::NextDay()
	{
		m_Day++;
		if (m_Day > DaysInMonth())
		{
			m_Day = 1;
			m_Month++;
		}
		if (m_Month > 12)
		{
			m_Month = 1;
			m_Year++;
		}
	}

	void CDate::AddDays(unsigned int num_days)
	{
		for (int i= 0; i < num_days; i++)
		{
			NextDay();
		}
	}


	void CDate::PreviousDay()
	{
		m_Day--;
		if (m_Day < 1)
		{
			m_Month--;
			if (m_Month < 1)
			{
				m_Month = 12;
				m_Year--;
			}
			m_Day = DaysInMonth();
		}


	}
	void CDate::SubtractDays(unsigned int num_days)
	{}

	bool CDate::operator==(CDate& other)
	{
		return m_Day == other.m_Day && 
			m_Month == other.m_Month &&
			m_Year == other.m_Year;
	}

	bool CDate::operator!=(CDate& other)
	{
		return !(*this == other);
	}

	bool CDate::operator<(CDate& other)
	{
		if (m_Year > other.m_Year)
			return false;

		if (m_Year < other.m_Year)
			return true;

		if (m_Month > other.m_Month)
			return false;

		if (m_Month < other.m_Month)
			return true;

		if (m_Day > other.m_Day)
			return false;

		if (m_Day < other.m_Day)
			return true;

		return false;
	}

	string CDate::ToString()
	{
		char buffer[64] = "";
		char format[] = "%u/%u/%u";

		sprintf(buffer,format, m_Day, m_Month, m_Year);

		return string(buffer);  
	}
}

[/code]
int m = month+89, d = day+89, y = year+89;
No. This makes no sense at all anyway.


See this line in your code:
int m = month, d = day, y = year;
I don't think it should exist. Work directly on the variables named month, day, year. When you call the class function tomorrow, it should change the Date object.

To a large extent, you've completely missed the point of classes. They're not just a place to put functions.
Last edited on
Ok I think i understand what you are saying. I actually talked to someone about this and he said to loop my tomorrow function 90 times. How would I do that?
1
2
3
4
for (int i = 0; i < 90; i++)
{
  // do something
}
Okay, it is working using this code

1
2
3
4
5
6
7
8
9
10
void Employee::calc_probationary_date()
{
	
	for (int i = 0; i < 90; i++)
	{
		void tomorrow();
	}
	
}


I now need to find a way to save the date and output that. Any ideas?
I now need to find a way to save the date and output that. Any ideas?


Have a Date object. Keep it. Use cout to output the values in it.
Yes, I've gotten that much, I just don't know how would I get the date object from the tomorrow() function though? It has it's own cout within the function itself. I'm sorry I'm a beginner programmer
The tomorrow function is a class function.

To call it, you must already have a Date object. That's how you're calling it.
I know, but anything I put into that little function above, it says undefined.

I have my #include for all my header files as well. Is there something easy I'm missing?


Here is a lot of my code


EMPLOYEE.cpp
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 "Employee.h"



Employee::Employee(string first, string last, int id, int m, int d, int y) : start_date(m,d,y)
{
	first_name = first;
	last_name = last;
	emp_id = id;
}



void Employee::print_employee()
{
	cout << "Employee id        : " << emp_id << endl;
	cout << "Employee First Name: " << first_name << endl;
	cout << "Employee Last Name : " << last_name << endl;
	cout << "Employee Start Date: ";  start_date.print_date();

}


void Employee::calc_probationary_date()
{
	
	for (int i = 0; i < 90; i++)
	{
		void tomorrow();
	}
}

Employee::~Employee(void)
{
}


EMPLOYEE.h

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
#pragma once
#include "Date.h" // needed for date objects

class Employee
{
	friend ostream &operator<<(ostream&, const Date&);
	friend istream &operator>>(istream&, Date&);

public:
	

	Employee(string,string,int,int,int,int);
	void calc_probationary_date();
	void print_employee();
	void print_date() const;
	void tomorrow();


	~Employee(void);
private:
	Date start_date; // Using an object of the Date class as a data member
	string first_name,last_name;
	int emp_id;
};




Date.h

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
#pragma once
#include <iostream>
#include <string>
using namespace std;

class Date
{
	friend ostream &operator<<(ostream&, const Date&);
	friend istream &operator>>(istream&, Date&);

public: // access specifiers interface is designed here
	Date(int=1,int=1,int=1900); // default constructor

	Date operator+(const Date &r); // overload assignment operator


	void print_date() const; // method to print the date 
	void print_military_date(); // i.e. 4-January-2016 or 4-Jan-16
	void print_full_date(); // i.e. Wednesday, March 16, 2016
	void yesterday();
	void tomorrow();



	~Date(void); // destructor
private:
	bool leapyear(int); // private method only used within the class
	int month, day, year, dow; // data members
	string day_string, month_string;
	void calc_dow();
	void get_day_of_week();
};



Date.cpp

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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "Date.h"
#include "Employee.h"
// implementation of the methods

Date::Date(int m, int d, int y)
{
	switch(m)
	{
	case 1: case 3: case 5: case 7: case 8: case 10: case 12:
		if((d <= 31) && (d > 0))
		{
			month = m;
			day = d;
			year = y;
			break;
		}
	case 4: case 6: case 9: case 11:
		if((d <= 30) && (d > 0))
		{
			month = m;
			day = d;
			year = y;
			break;
		}

	case 2: // February
		if((leapyear(y)) && (d <= 29 && d > 0))
		{
			month = m;
			day = d;
			year = y;
			break;
		}
		else if ((!(leapyear(y))) && (d <= 28) && (d > 0))
		{
			month = m;
			day = d;
			year = y;
			break;
		}
	default:
		cerr << "Invalid Date! ";
		month = m;
		day = d;
		year = y;
		print_date();
		break;
	} // end switch
}

void Date::print_date() const
{
	cout << month << "-" << day << "-" << year << endl;
}

void Date::print_military_date()
{
	// 4-January-2016


	string monthname;
	monthname = (month);

	if (month == 1)
		monthname = "January";
	if (month == 2)
		monthname = "February";
	if (month == 3)
		monthname = "March";
	if (month == 4)
		monthname = "April";
	if (month == 5)
		monthname = "May";
	if (month == 6)
		monthname = "June";
	if (month == 7)
		monthname = "July";
	if (month == 8)
		monthname = "August";
	if (month == 9)
		monthname = "September";
	if (month == 10)
		monthname = "October";
	if (month == 11)
		monthname = "November";
	if (month == 12)
		monthname = "December";
	
	cout << day << "-" << monthname << "-" << year;
}

void Date::print_full_date()
{

}

void Date::calc_dow()
{
	int m = month, d = day, y = year; //preserve orignal values
	dow = (d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7;
	// dow is an integer value from 0 - 6, 0 = Sunday, 1 = Monday, etc...
}

void Date::get_day_of_week()
{
	if(dow == 0)
		day_string = "Sunday";
	else if (dow == 1)
		day_string = "Monday";
	else if (dow == 2)
		day_string = "Tuesday";
	else if (dow == 3)
		day_string = "Wednesday";
	else if (dow == 4)
		day_string = "Thursday";
	else if (dow == 5)
		day_string = "Friday";
	else
	     day_string = "Saturday";

}




void Date::yesterday()
{
	int prevday, prevmonth, prevyear;
	switch(month)
	{
	case 2:case 4:case 6:case 8:case 9:case 11:
		if (day==1)
		{
			prevmonth = month -1;
			prevday = 31;
			prevyear = year;
			break;
		}
		else
		{
			prevmonth = month;
			prevday = day -1;
			prevyear = year;
			break;
		}

	case 5:case 7:case 10: case 12:
		if(day == 1)
		{
			prevmonth = month - 1;
			prevday = 30;
			prevyear = year;
			break;
		}
		else
		{
			prevmonth = month;
			prevday = day -1;
			prevyear = year;
			break;
		}

	case 3:
		if(leapyear(year) && day == 1)
		{
			prevmonth = month -1;
			prevday = 29;
			prevyear = year;
			break;
		}
		else if(!leapyear(year) && day == 1)
		{
			prevmonth = month -1;
			prevday = 28;
			prevyear = year;
			break;
		}
		else
		{
			prevmonth = month;
			prevday = day -1;
			prevyear = year;
			break;
		}

	case 1:
		if(day == 1)
		{
			prevmonth = 12;
			prevday = 31;
			prevyear = year -1;
			break;
		}
		else
		{
			prevmonth = month;
			prevday = day -1;
			prevyear = year;
			break;
		}
	} // end switch

	cout << "Yesterday was " << prevmonth << "-" << prevday << "-" << prevyear << ".";
}





void Date::tomorrow()
{
	{
	int m = month, d = day, y = year;

	switch(m)


	{case 1: case 3: case 5: case 7: case 8: case 10: case 12:
		if(d < 30)
		{d ++; break;}

		else if((d <= 31) && (m < 11))
		{m ++;d = 1;break;}

		else if((d <= 31) && (m >= 12))
		{y ++;m = 1;d = 1;break;}


	case 4: case 6: case 9: case 11:
		if(d < 29)
		{d ++;break;}

		else if(d <= 30)
		{m ++;d = 1;break;
		}


	case 2: //February
		if((leapyear(y)) && (d < 29))
		{d ++;break;}

		else if((!(leapyear(y))) && (d < 28))
		{d ++;break;}

		else if((leapyear(y)) && (d >= 29))
		{m ++;d = 1;break;}

		else if ((!(leapyear(y))) && (d >= 28))
		{m ++; d = 1; break;}


	}
	cout << m << "-" << d << "-" << y << endl;
	}
}




bool Date::leapyear(int y)
{
	if(y % 100 == 0)
		return (y % 400 == 0);
	else
	    return (y % 4 == 0);
}


istream &operator>>(istream &input, Date &r)
{ // Date format 03-21-2016
	input >> r.month;
	input.ignore();
	input >> r.day;
	input.ignore();
	input >> r.year;

return input;

}

Date::~Date(void)
{
}


DateTester.cpp

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
#include "Date.h"
#include "Employee.h"
#include "HourlyEmployee.h"


int main()
{
	Date today;
	
	cout << "Enter today's date mm-dd-yyyy ";
	cin >> today;

	today.print_date();

	Employee e1("Elmer","Flopshingle", 1001, 3, 15, 2012), e2 = e1;
	e1.print_employee();
	cout << endl;
	e2.print_employee();


	HourlyEmployee h1("Willie","Smokestack", 1002, 4, 21, 2015, 15.00, 40);

	h1.print_employee();
	cout << h1.calc_pay() << endl;


return 0;
}



There is more code but this is the relevant stuff. Can you tell me what exactly I'm doing wrong with my calc_probationary_date() ? Thank you for all your help so far
Date::tomorrow() is a class function of a Date object. Employee::calc_probationary_date is a class function of a different kind of object.

You can call Date object functions on a Date object; you can't call them on an Employee object.

Create a Date object with today's date, call tomorrow on it 90 times, ask it to print itself out.
Can I use my start_date object in Employee.cpp? I just don't understand how the code would look. This is my first year trying to learn programming
Topic archived. No new replies allowed.