-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_date.cpp
66 lines (59 loc) · 1.39 KB
/
1_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
//
// Created by Varsha on 21-03-2023.
//
/* Define a class date that contains details like year, month and date. Write a C++ program
* to check the validity of date you have entered and display the next day*/
#include <iostream>
using namespace std;
class date {
private:
int year;
int month;
int day;
int max_days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int validity = 1;
public:
void init() {
cout<<"Enter the date in the format DD-MM-YYYY:";
cin>>day>>month>>year;
}
void display() {
cout<<"The next day is: "<<day<<"-"<<month<<"-"<<year<<endl;
}
void is_leap() {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
max_days[2] = 29;
}
}
void valid() {
is_leap();
if (year <= 0 || month < 1 || month > 12 || day < 1 || day > max_days[month]) {
validity = 0;
}
}
void next_day() {
valid();
if (!validity) {
cout << "Invalid date" << endl;
return;
}
if (day < max_days[month]) {
day++;
} else {
day = 1;
if (month < 12) {
month++;
} else {
month = 1;
year++;
}
}
display();
}
};
int main() {
date d1;
d1.init();
d1.next_day();
return 0;
}