How do I increment my Year type

I need to increment year in my add_day() function. Is there any way to do that without making a function specifically for that purpose?

The function is located in Chrono.cpp

Chrono.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
#include "std_lib_facilities.h"

namespace Chrono
{
	enum class Month
	{
		jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
	};
	class Year
	{
		static const int min = 1800;
		static const int max = 2200;

	public:
		class Invalid{};

		Year(int x);

		int year() { return y; }

	private:
		int y;
	};
	class Date
	{
	public:
		class Invalid{};

		Date();
		Date(Month m, int d, Year y);

		void add_day(int n);
		Month month() const { return m; }
		int day() const { return d; }
		Year year() const { return y; }

	private:
		Year y;
		Month m;
		int d;
	};

	int days_in_month(Month m, Year y);

	bool is_date(Month m, int d, Year y);
	bool leapyear(Year y);

	ostream& operator<<(ostream& os, const Date& d);
}



Chrono.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


namespace Chrono
{
	Date::Date()
		:m{ Month::jan }, d{ 1 }, y{1970}
	{

	}
	Date::Date(Month m, int d, Year y)
		: m{ m }, d{ d }, y{y}
	{
		if (!is_date(m, d, y)) throw Invalid{};
	}
	Year::Year(int x)
		:y{ x }
	{
		if (x < min || max < x) throw Invalid{};
	}

	void Date::add_day(int n)
	{
		
		for (int i = 0; i < n; i++)
		{
			if (d == days_in_month(m, y))
			{
				d = 1;
				if (m == Month::dec) //increments year if adding to last day of last month in year.
				{
					//TODO: Increment year
					m = Month::jan;
				}
				else // Otherwise, only month is incremented
					m = Month(int(m) + 1);
			}
			else
				d++;
		}
	}

	ostream& operator<<(ostream& os, const Date& d)
	{
		return os << "(" << d.month() << " / " << d.day() << " / " << d.year() << ")";
	}

	int days_in_month(Month m, Year y)
	{
		int amount_of_days = 31;

		switch (m)
		{
		case Month::feb:
			amount_of_days = (leapyear(y)) ? 29 : 28;
			break;
		case Month::apr: case Month::jun: case Month::sep: case Month::nov:
			amount_of_days = 30;
			break;
		}
		return amount_of_days;
	}

	bool is_date(Month m, int d, Year y)
	{
		if (d < 0 || days_in_month(m, y) < d) return false;
		if (y.year() < 0) return false;
		if (m < Month::jan || Month::dec < m) return false;
		
		return true;
	}
	bool leapyear(Year y)
	{
		if (y.year() % 4 != 0) return false;
		if (y.year() % 100 == 0)
		{
			if (y.year() % 400 != 0) return false;
		}
		return true;
	}
}


Source.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
#include "std_lib_facilities.h"
#include "Chrono.h"

int main()
{
	try
	{
		Chrono::Date today = Chrono::Date(Chrono::Month::jun, 25, Chrono::Year{ 1978 });
		Chrono::Date tomorrow = today;
		tomorrow.add_day(1);
		cout << today << endl << tomorrow << endl;

		keep_window_open();
	}
	catch (Chrono::Date::Invalid)
	{
		cerr << "Invalid date." << endl;
		keep_window_open("~1");
		return 1;
	}
	catch (exception& e)
	{
		cerr << e.what() << endl;
		keep_window_open("~2");
		return 2;
	}
}
Last edited on
You could do the same thing that you did to increment the day. That is, add an add_year method to Chrono::Year.
I see, so there's no short way that I can do. I have to overload an operator or make a separate function?
Why not just y+=1?
Wouldn't that not compile either since I'm trying to convert a Year to a void?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void Date::add_day(int n)
	{
		
		for (int i = 0; i < n; i++)
		{
			if (d == days_in_month(m, y))
			{
				d = 1;
				if (m == Month::dec) //increments year if adding to last day of last month in year.
				{
					y = y.add_year(1);
					m = Month::jan;
				}
				else // Otherwise, only month is incrmented
					m = Month(int(m) + 1);
			}
			else
				d++;
		}
	}
Last edited on
Oh, I did not realize you were looking to increment the year in the add_day function. Follow what Duoas said then.
I would then have to overload '+' to work for my Year type, which I can't do if I don't know how to increment years in the first place.
Last edited on
Year::y is an int... += should be no problem.
Last edited on
I'm sorry, that was sloppiness on my part. 'y' refers to Date::y
Last edited on
There is only one parameter to your Date::add_day function: (int n)
You should easily be able to add an add_year method to Chrono::Year and use += to modify the private int variable that keeps track of the year.
Last edited on
I see what you mean! I thought you meant write y++ into the add_day(), not y++ into add_year().

I got this after, and it compiles.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
	int Year::add_year(int n)
	{
		return y += n;
	}
	void Date::add_day(int n)
	{
		
		for (int i = 0; i < n; i++)
		{
			if (d == days_in_month(m, y))
			{
				d = 1;
				if (m == Month::dec) //increments year if adding to last day of last month in year.
				{
					y = y.add_year(1);
					m = Month::jan;
				}
				else // Otherwise, only month is incrmented
					m = Month(int(m) + 1);
			}
			else
				d++;
		}
	}
Last edited on
Topic archived. No new replies allowed.