Write a program that reads several dates, and for each one tells if it is correct or not according to the Gregorian calendar. Remember leap years.
Each date consists of three integer numbers, corresponding to the day, month and year, respectively. All years are between 1800 and 9999.
My program gives correct output with almost all cases. However in one or more (which I do not know) it gives me an incorrect answer. I have looked at my code several times and I am unable to find an error.
#include <iostream>
usingnamespace std;
void date (int d, int m, int y) {
while (cin >> d >> m >> y) {
if (y >= 1800 and y <= 9999) {
if (m >= 1 and m <= 12) {
if (m == 1 or m == 3 or m == 5 or m == 7 or m == 8 or m == 10 or m == 12) {
if (d >= 1 and d <= 31) cout << "Data Correcta" << endl;
else cout << "Data Incorrecta" << endl;
}
elseif (m == 4 or m == 6 or m == 9 or m == 11) {
if (d >= 1 and d <= 30) cout << "Data Correcta" << endl;
else cout << "Data Incorrecta" << endl;
}
elseif (y%4 == 0 and y%100 == 0) {
y = y/100;
if (y%4 == 0) {
if (d >= 1 and d <= 29) cout << "Data Correcta" << endl;
else cout << "Data Incorrecta" << endl;
}
else {
if (d >= 1 and d <= 28) cout << "Data Correcta" << endl;
else cout << "Data Incorrecta" << endl;
}
}
else {
if (d >= 1 and d <= 28) cout << "Data Correcta" << endl;
else cout << "Data Incorrecta" << endl;
}
}
else cout << "Data Incorrecta" << endl;
}
else cout << "Data Incorrecta" << endl;
}
}
int main () {
int d,m,y;
date (d,m,y);
}
#include <iostream>
usingnamespace std;
void date () {
int d, m, y;
while (cin >> d >> m >> y) {
if (y >= 1800 and y <= 9999) {
if (m >= 1 and m <= 12) {
if (m == 1 or m == 3 or m == 5 or m == 7 or m == 8 or m == 10 or m == 12) {
if (d >= 1 and d <= 31) cout << "Data Correcta" << endl;
else cout << "Data Incorrecta" << endl;
}
elseif (m == 4 or m == 6 or m == 9 or m == 11) {
if (d >= 1 and d <= 30) cout << "Data Correcta" << endl;
else cout << "Data Incorrecta" << endl;
}
elseif (y%4 == 0) {
if (y%100 == 0) {
y = y/100;
if (y%4 == 0) {
if (d >= 1 and d <= 29) cout << "Data Correcta" << endl;
else cout << "Data Incorrecta" << endl;
}
else {
if (d >= 1 and d<= 28) cout << "Data Correcta" << endl;
else cout << "Data Incorrecta" << endl;
}
}
else {
if (d >= 1 and d <= 29) cout << "Data Correcta" << endl;
else cout << "Data Incorrecta" << endl;
}
}
else {
if (d >= 1 and d <= 28) cout << "Data Correcta" << endl;
else cout << "Data Incorrecta" << endl;
}
}
else cout << "Data Incorrecta" << endl;
}
else cout << "Data Incorrecta" << endl;
}
}
int main () {
date ();
}
As ne555 said "the paramters of your function are irrelevant". Although it does work you should initialize your variable in main so they have a value and not undefined values. Otherwise define those variables in the function.
In the function the use of the words "and" and "or" are incorrect they should be && and ||.
The changes I made is to comment out line 17 and changed line 16 to elseif ((y % 4 == 0) && !(y % 100 == 0) || (y % 400 == 0)) and the program worked every time I entered a leap year and said invalid otherwise.
> the use of the words "and" and "or" are incorrect they should be && and ||.
the use of the keywords and, or, not, bitor, etc., is perfectly valid and even encouraged, so people stop asking what «||» means.