Program almost working correctly

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.
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
#include <iostream>
using namespace 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;
				}
				else	if (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;
					}
				else	if (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);
}
Last edited on
I'll give you one case
29 2 2016



PS:
1
2
void date (int d, int m, int y) {
	while (cin >> d >> m >> y) {
the paramters of your function are irrelevant
Last edited on
Now program works, thanks

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
#include <iostream>
using namespace 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;
				}
				else	if (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;
					}
					else	if (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 ();
}
Hello Oriol Serrabassa,

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
else if ((y % 4 == 0) && !(y % 100 == 0) || (y % 400 == 0)) and the program worked every time I entered a leap year and said invalid otherwise.

Hope that helps,

Andy
> 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.
Topic archived. No new replies allowed.