Logical Date Check Program

Hi guys!
I have to write a program inputs dates as a sequences of three integers, and checks to see if they are valid dates.

I'm having trouble with February, which is to follow these guidelines:

February usually has 28 day. However, when the year is evenly divisible by 4, it often has 29 days. The exception to the 4-year rule is when the year is evenly divisible by 100. When that happens, we stick with the usual 28 days. However, whenever the year is evenly divisible by 400, we are back at 29 days again. (You can thank Pope Gregory for this...)

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
#include <iostream>
using namespace std;

int main(void)
{
	// 1 Introduction - Instructions and Input
	int month, day, year;
	cout << endl << "Input a date as three numbers separated by spaces (month day year)" << endl;
	cout << "\t\t-> "; // this is so it looks neater
	cin >> month >> day >> year;
	cin.ignore(99,'\n');	
	// Non Numeric Input
	if ( !cin )
	{
		cin.clear();
		cin.ignore(99,'\n');

		cout << endl << "Non-numeric input: Program terminating." << endl;
		cout << "Press [ENTER] to finish...";
		cin.ignore(99,'\n');
		return 0;
	}
	// Date Limits
	if ( month < 0 || month > 12 )	// Months should be in the range from 1 (January) to 12 (December)
	{
		cout << endl << "Date of range: Program terminating." << endl;
		cout << "Reason: Months should be in the range from 1 (January) to 12 (December)." << endl;
		cout << "Press [ENTER] to finish...";
		cin.ignore(99,'\n');
		return 0;
	}
	if ( year < 0 ) // Years should be non-negative.
	{
		cout << endl << "Input out of range: Program terminating." << endl;
		cout << "Reason: Years should be non-negative." << endl;
		cout << "Press [ENTER] to finish...";
		cin.ignore(99,'\n');
		return 0;
	}
	if ( day < 0 ) // Days also should be non-negative. 
	{
		cout << endl << "Date of range: Program terminating." << endl;
		cout << "Reason: Days should be non-negative." << endl;
		cout << "Press [ENTER] to finish...";
		cin.ignore(99,'\n');
		return 0;
	}
	// January, March, May, July, August, October and December have 31 days.
	if ( month = 1,3,5,7,8,10,12 )
	{
		if ( day > 31 )
		{
			cout << "Invalid Input, Please check your date and try again.";
			cin.ignore(99,'\n');
			return 0;
		}
	}
	// April, June, September and November have 30 days
	if ( month = 4,6,9,11 )
	{
		if ( day > 30 )
		{
			cout << "Invalid Input, Please check your date and try again.";
			cin.ignore(99,'\n');
			return 0;
		}
	}

//// Having trouble with February 
	if ( month = 2 )
	{
// Don't know what to put here...
	}
	// I figure that if none of the IF Statements are True, then the date should be valid
	cout << endl << "Date is Valid. :)" << endl;
	cout << "Press [ENTER] to finish..." << endl;

	// Finish
	cin.ignore(99,'\n');
	return 0;
}
Figure out how many days are in February. You can put this in a variable named "daysinmonth" or something.

Once you have that variable set appropriately, just check:

1
2
3
4
if(day > daysinmonth)
{
  // print error to user
}
If you have studied functions, you might want to make one that tells you whether a given year is a leap year. (If you have not studied functions, then just put the code that determines whether it is a leap year first in your function, after obtaining the date to check.)

If it is a leap year, then February has 29 days. Else it has only 28.

Some other help:

Line 4: The void is incorrect in C++. Use int main().

Lines 11, 19-20, etc: Nice one! (I'm nearly crying, I'm so proud of you!)

Line 21: Consider return 1; if you are terminating due to error(s).

Line 24: You have a fencepost error that accepts months with values from 0 to 12, inclusive.

Line 40: Again, this is a fencepost error. The day could be zero. (But you want to to be greater than zero.)

Lines 49, 59: These don't do what you think they do. You cannot abbreviate this kind of condition. You might want to use a switch control instead:
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
	switch (month)
	{
		// January, March, May, July, August, October and December have 31 days.
		case 1: case 3: case 5: case 7: case 8: case 10: case 12:
			if (day > 30)
			{
				...
			}
			break;

		// April, June, September and November have 30 days
		case 4: case 6: case 9: case 11:
			if (day > 31)
			{
				...
			}
			break;

		// February has 29 days in leap years, 28 days elsewise
		case 2:
			// Figure out if it is, actually, a leap year
			...
			if ((is_leap_year && (day > 29)) || (day > 28))
			{
				...
			}
			break;
	}

	// Date must be valid
	cout << endl << "Date is Valid. :)" << endl;
	...

Hope this helps.
Thanx guys for the support! I was taking a little break but I'll get on it right away.
You've both been so helpful! :3

I'll try what you've guys taught me
Topic archived. No new replies allowed.