Date check + EoF

Something in this code is giving me positive results (good date) when an incorrect date is entered. It only answers correctly for year, and checks leap year correctly, or date is bad in . Also how would i set this to end when ctrl+z is input?

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
#include<iostream> 
#include<conio.h>
#include<string>
using namespace std;

void getDate(int& mon, int& day, int& year);
int ckDate(int mon, int day, int year);	
void displayMsg(int message);

int main()
{
	int mon, day, year, message;
	string date;
	

	while(cin)
     {
		getDate(mon,day,year);			// call function getDate
		int message=ckDate(mon,day,year);		// cal function ckDate
		cout << mon << "/" << day << "/" << year << "-";
		displayMsg(message);					//call function displayMsg
		
     }
	
	_getch();
	return 0;

}  

void getDate(int& mon, int& day, int& year)
{	cout << "\n\nEnter a date (mm/dd/yyyy): ";
	cin >> mon;
	cin.ignore(100, '/');
	cin	>> day;
	cin.ignore(100, '/');
	cin >> year;
	
	
}	

int ckDate(int mon, int day, int year)
{	
	
	 bool isLeapYear = false;
	 int resultNum = 0;

           if ((year <= 999) || (year > 10000))
			resultNum = 1;//attempting to change from multiple return statements with an integer to a variable resultNum.

               
           
           if ((year % 4 == 0 && year % 100 != 0) || (year % 4 == 0 && year % 100 == 0 && year % 400 == 0))
               isLeapYear = true;

           
   
           if (day < 1 || day > 31) 
					   resultNum = 3;
				//resultNum = 3; attempting to assign variable to 3 
				//return resultNum;//returning it to displayMsg

           if (isLeapYear && (day < 1 || day > 29)) 
					   resultNum = 5;
           if (!isLeapYear && (day < 1 || day > 28)) 
					   resultNum = 6;

               
		   if (mon < 1 || mon > 12) 
						resultNum = 2;
				   return resultNum;//appears to work correctly

          	return resultNum;//appears to work correctly	
				return 0;
	}



void displayMsg(int message)
{
	switch(message)
	{
	case 0:
		cout << " Good Date";
		break;
	case 1:
		cout << " Bad Year";
		break;
	case 2:
		cout << " Bad Month";
		break;
	case 3:
		cout << " Bad Day not 1-31";
		break;
	case 4:
		cout << " Bad Day not 1-30";
		break;
	case 5:
		cout << " Bad Day not 1-29";
		break;
	case 6:
		cout << " Bad Day not 1-28";
		break;
	}

}
Last edited on
fixed, just need to know how to end it on ctrl+z
Topic archived. No new replies allowed.