Date Class Assignment!

So I have an assignment to make a program that calculates the today's date. It also has to calculate tomorrow's and yesterday's. I have the bulk of the program written correctly I think, I just can't figure out how on earth to write the yesterday and tomorrow methods. Can you guys give me something to start out with or any tips or help? Thank you!

Here is the date header 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
 #pragma once
#include <iostream>
#include <string>
using namespace std;

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

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



Here is the 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
#include "Date.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()
{
	// HELP HERE
}





void Date::tomorrow()
{
	// HELP HERE 
}




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

Date::~Date(void)
{
}

Last edited on
Topic archived. No new replies allowed.