-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathdate.cpp
More file actions
94 lines (76 loc) · 2.78 KB
/
date.cpp
File metadata and controls
94 lines (76 loc) · 2.78 KB
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
//*****************************************************************************************************
// Date Class Implementation File
//
// This class implementation file defines the methods (member functions) of the Date class.
//
// Other files required:
// 1. date.h - header file for Date class
//
//*****************************************************************************************************
#include "date.h"
#include <iostream>
//*****************************************************************************************************
// default constructor initializes member variables
Date::Date() {
day = 1;
month = 1;
year = 2000;
}
//*****************************************************************************************************
Date::~Date() {
std::cout << "\nDate object destroyed\n";
}
//*****************************************************************************************************
// checks for valid date (leap years, days in month) and sets date
void Date::inputDate() {
std::cout << "\nEnter Year: ";
std::cin >> year;
while (year < 2022 || year > 2100) {
std::cerr << "\nInvalid\n\n";
std::cout << "Enter Year(2022- ): ";
std::cin >> year;
}
setYear(year);
std::cout << "Enter Month: ";
std::cin >> month;
while (month < 1 || month > 12) {
std::cerr << "\nInvalid\n\n";
std::cout << "Enter Month(1-12): ";
std::cin >> month;
}
setMonth(month);
std::cout << "Enter Day: ";
std::cin >> day;
if (month == 1 || month == 3 || month == 5 || month == 7 ||
month == 8 || month == 10 || month == 12) {
while (day < 1 || day > 31) {
std::cerr << "\nInvalid\n\n";
std::cout << "Enter Day(1-31): ";
std::cin >> day;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
while (day < 1 || day > 30) {
std::cerr << "\nInvalid\n\n";
std::cout << "Enter Day(1-30): ";
std::cin >> day;
}
} else if (year % 4 == 0) { // February :: tests leap years
while (day < 1 || day > 29) {
std::cerr << "\nInvalid\n\n";
std::cout << "Enter Day(1-29): ";
std::cin >> day;
}
} else {
while (day < 1 || day > 28) {
std::cerr << "\nInvalid\n\n";
std::cout << "Enter Day(1-28): ";
std::cin >> day;
}
}
setDay(day);
}
//*****************************************************************************************************
void Date::displayDate() {
std::cout << getMonth() << "/" << getDay() << "/" << getYear();
}
//*****************************************************************************************************