URGENT!! Validate Date - user defined functions.

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
How does the valid variable get a value before you pass it to displayMsg()?
Thats where I'm getting the error from. Also I'm getting error from check variable in ckDate().
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
No. I meant I'm also getting error in ckDate() too. Not related to displayMsg().
So ckDate() returns the check variable value. Where do you put it? (Or where do you save it?)
to displayMsg(int check).
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?
I updated the code. Now I'm getting Good Date message even if I enter invalid date (0/22/2013)
Updated it how? Show code.

What does it do for your original test case of 13/32/999?
I updated the code in the original post. Good Date message when I enter 13/32/999
Last edited on
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?
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.