URGENT!! Validate Date - user defined functions.

Apr 21, 2013 at 10:45pm
I am writing an program to validate the date. When I run the program, even the invalid dates (13/32/999) gets validated. Please help!!

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <conio.h>

using namespace std;

void getDate( int&, int&, int&);  
int ckDate( int, int, int);
void displayMsg( int);

int main(){
	int month;
	int day;
	int year;
	int check;

	while(cin){
		getDate(month, day, year);
		check = ckDate(month, day, year);
		displayMsg(check);
	}

	_getch();
	return 0;
}

void getDate(int& m, int& d, int& y){
	cout << "Enter a date (mm/dd/yyyy): ";
	cin >> m >> d >> y;
}

int ckDate (int m, int d, int y){
	int check = 0;

		switch(check){
			case 1:
				if (y < 999 && y > 10000)
				{
					check = 1;
				}	
			break;

			case 2:		
				if (m < 1 || m > 12)
				{
					check = 2;
				}
			break;

			case 3:
				while (m == 1 || m == 3 || m == 5 || m == 7 || m == 8|| m == 10 || m == 12)
					if  (d < 1 || d > 31)
					{
						check = 3;
					}
			break;

			case 4:
				while (m == 4 ||m == 6 || m == 9 || m == 11)
					if (d < 1 || d > 30)
					{
						check = 4;
					}
				break;

			case 5:
				while (m == 2){
					if (y % 400 == 0)
					{
						if (y % 100 == 0 && y % 4 == 0)
						{
							if (d < 1 && d > 29)
							{
								check = 5;
							}
						}
					}
					else if (d < 1 && d > 28)
					{
						check = 6;
					}
				}
			break;
			
			default: 
				check = 0;
		}

	return check;
}

void displayMsg(int check){
	
	if (check == 0)
	{
		cout << "Good Date\n";
	}
	else if (check == 1)
	{
		cout << "Bad Year\n";
	}
	else if (check == 2)
	{
		cout << "Bad Month\n";
	}
	else if (check == 3)
	{
		cout << "Bad Day not 1-31\n";
	}
	else if (check == 4)
	{
		cout << "Bad Day not 1-30\n";
	}
	else if (check == 5)
	{
		cout << "Bad Day not 1-29\n";
	}
	else if (check == 6)
	{
		cout << "Bad Day not 1-28\n" ;
	}
}
Last edited on Apr 22, 2013 at 4:00am
Apr 22, 2013 at 3:32am
How does the valid variable get a value before you pass it to displayMsg()?
Apr 22, 2013 at 3:34am
Thats where I'm getting the error from. Also I'm getting error from check variable in ckDate().
Apr 22, 2013 at 3:40am
Also I'm getting error from check variable in ckDate().


How are you getting the error from the check variable into the valid variable? Have you learned the assignment statement in your C++ class yet?
Last edited on Apr 22, 2013 at 3:41am
Apr 22, 2013 at 3:41am
No. I meant I'm also getting error in ckDate() too. Not related to displayMsg().
Apr 22, 2013 at 3:45am
So ckDate() returns the check variable value. Where do you put it? (Or where do you save it?)
Apr 22, 2013 at 3:53am
to displayMsg(int check).
Apr 22, 2013 at 3:58am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int SomeFunction()
{
    int ReturnValue;

    /* Calculate ReturnValue some how. */

    return ReturnValue;
}

int main()
{
    int SomeVariable;

    SomeVariable = SomeFunction();

    /* Now SomeVariable has the value returned from SomeFunction in the ReturnValue variable. */
}


Do you see how that applies to your situation?
Apr 22, 2013 at 4:01am
I updated the code. Now I'm getting Good Date message even if I enter invalid date (0/22/2013)
Apr 22, 2013 at 4:04am
Updated it how? Show code.

What does it do for your original test case of 13/32/999?
Apr 22, 2013 at 4:05am
I updated the code in the original post. Good Date message when I enter 13/32/999
Last edited on Apr 22, 2013 at 4:06am
Apr 22, 2013 at 4:12am
In ckDate(), what value is the check variable start with?

So which case in the switch statement is going to be used?

So what value is the check variable going to end with?

So which output message will be displayed in displayMsg()?

By the way, in ckDate(), you have:

1
2
3
4
				if (y < 999 && y > 10000)
				{
					check = 1;
				}	


For which values of y will the if condition ever be true?
Apr 22, 2013 at 4:25am
I changed switch statements to if statements to simplify the ckDate()

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
int ckDate (int m, int d, int y){
	int check = 0;

				if (y < 999 && y > 10000)
					check = 1;
				
				if (m < 1 || m > 12)
					check = 2;

				while (m == 1 || m == 3 || m == 5 || m == 7 || m == 8|| m == 10 || m == 12)
					if  (d < 1 || d > 31)
					{
						check = 3;
					}
	
				while (m == 4 ||m == 6 || m == 9 || m == 11)
					if (d < 1 || d > 30)
					{
						check = 4;
					}
				
				while (m == 2){
					if (y % 400 == 0)
					{
						if (y % 100 == 0 && y % 4 == 0)
						{
							if (d < 1 && d > 29)
							{
								check = 5;
							}
						}
					}
					else if (d < 1 && d > 28)
					{
						check = 6;
					}
				}
Topic archived. No new replies allowed.