Date Validation

I am looking to tell if the date inputed by the user is valid or not. My dateValidation function does not seem to be doing what I would like it to.

I was hoping to get something telling me that a date like 2/29/2015 or 45/45/1400 would be incorrect.

Any direction I should take to resolve this issue is appreciated. Thanks for taking your time to look at my code.

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
using namespace std;

class date
{
    public:
        void setDate(int m, int d, int y);
        bool dateValidation();
        void printDate() const;
        void printLongDate() const;
        bool leapYear(int y) const;
        date (int m = 1, int d = 1, int y = 1990);
    private:
        int month;
        int day;
        int year;
};

int main()
{
    date calendar;
    int m1, d1, y1;

    cout << "Please enter a date: ";
    cin >> m1 >> d1 >> y1;

    calendar.setDate(m1, d1, y1);

    calendar.leapYear(y1);

    calendar.dateValidation();

    calendar.printDate();
    calendar.printLongDate();



    // your code goes here
    return 0;
}

//Class Object that sets the date
void date::setDate(int m, int d, int y)
{

       if (m >= 1 && m <= 12)
        month = m;
    else
        month = 1;

    if (d >= 1 && d <= 31)
        day = d;
    else
        day = 1;

    if (y >= 1582)
        year = y;
    else
        year = 1990;

}

bool date::leapYear(int y) const
{
    bool leapYear = (y % 4 == 0 && y % 100 !=0) || (y % 400 == 0);

       if(leapYear)
           return true;
       else
           return false;
}

bool date::dateValidation()
{
  if (! (1 >= month && month<=12) )
     return false;
  if (! (1<= day && day<=31) )
     return false;
  if ( (day==31) && (month==2 || month==4 || month==6 || month==9 || month==11) )
     return false;
  if ( (day==30) && (month==2) )
     return false;
  if ( (month==2) && (day==29) && (year%4!=0) )
     return false;
  if ( (month==2) && (day==29) && (year%400==0) )
     return true;
  if ( (month==2) && (day==29) && (year%100==0) )
     return false;
  if ( (month==2) && (day==29) && (year%4==0)  )
     return true;


}

//Class Object that prints
void date::printDate() const
{
    if(month <= 9 && day <= 9)
        cout << "The date you entered is: " << "0" << month << "-" << "0" << day << "-" << year;
    else if (month <= 9 && day >= 9)
        cout << "The date you entered is: " << "0" << month << "-" << day << "-" << year;
    else if (month >= 10 && day <=9)
        cout << "The date you entered is: " << month << "-0"  << day << "-" << year;
    else
    cout << "The date you entered is: " << month << "-" << day << "-" << year;
    cout << endl;

}

void date::printLongDate() const
{
    cout << "You have entered ";
    if(month <= 1)
        cout << "January " << day << ", " << year;
    else if(month <= 2)
        cout << "February " << day << ", " << year;
    else if(month == 3)
        cout << "March " << day << ", " << year;
    else if(month == 4)
        cout << "April " << day << ", " << year;
    else if(month == 5)
        cout << "May " << day << ", " << year;
    else if(month == 6)
        cout << "June " << day << ", " << year;
    else if(month == 7)
        cout << "July " << day << ", " << year;
    else if(month == 8)
        cout << "August " << day << ", " << year;
    else if(month == 9)
        cout << "September " << day << ", " << year;
    else if(month == 10)
        cout << "October " << day << ", " << year;
    else if(month == 11)
        cout << "November " << day << ", " << year;
    else
        cout << "December " << day << ", " << year;
    cout << endl;
}

//Constructor with parameters
date::date (int m, int d, int y)
{
    month = m;
    day = d;
    year = y;
}
Went through your code and fixed your if statement starting on line 75. I changed it to:
1
2
3
4
	if (month <= 1 || month >= 12)
	{
		cout << "invalid date";
	} 

also changed the function type to void.


If someone inputs a month that is not between those parameters it will print you : "invalid date", however there are a lot more issues here besides that. You really need to put { } around the body of your if statements or else if statements. The statements aren't interpreted correctly if you don't have them. There are some other things I was thinking about changing but wasn't sure if I answered your question how you wanted. Compile the code below and you can kind of see what I am talking about

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
using namespace std;

class date
{
public:
	void setDate(int m, int d, int y);
	void dateValidation();
	void printDate() const;
	void printLongDate() const;
	bool leapYear(int y) const;
	date(int m = 1, int d = 1, int y = 1990);
private:
	int month;
	int day;
	int year;
};

int main()
{
	date calendar;
	int m1, d1, y1;

	cout << "Please enter a date: ";
	cin >> m1 >> d1 >> y1;

	calendar.dateValidation();

	calendar.setDate(m1, d1, y1);

	calendar.leapYear(y1);

	calendar.printDate();
	calendar.printLongDate();


	system("pause");
	// your code goes here
	return 0;
}

//Class Object that sets the date
void date::setDate(int m, int d, int y)
{

	if (m >= 1 && m <= 12)
		month = m;
	else
		month = 1;

	if (d >= 1 && d <= 31)
		day = d;
	else
		day = 1;

	if (y >= 1582)
		year = y;
	else
		year = 1990;

}

bool date::leapYear(int y) const
{
	bool leapYear = (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);

	if (leapYear)
		return true;
	else
		return false;
}

void date::dateValidation()
{
	if (month <= 1 || month >= 12)
	{
		cout << "invalid date";
	}
	/*
	if (!(1 <= day && day <= 31))
		return false;
	if ((day == 31) && (month == 2 || month == 4 || month == 6 || month == 9 || month == 11))
		return false;
	if ((day == 30) && (month == 2))
		return false;
	if ((month == 2) && (day == 29) && (year % 4 != 0))
		return false;
	if ((month == 2) && (day == 29) && (year % 400 == 0))
		return true;
	if ((month == 2) && (day == 29) && (year % 100 == 0))
		return false;
	if ((month == 2) && (day == 29) && (year % 4 == 0))
		return true;
*/

}

//Class Object that prints
void date::printDate() const
{
	if (month <= 9 && day <= 9)
		cout << "The date you entered is: " << "0" << month << "-" << "0" << day << "-" << year;
	else if (month <= 9 && day >= 9)
		cout << "The date you entered is: " << "0" << month << "-" << day << "-" << year;
	else if (month >= 10 && day <= 9)
		cout << "The date you entered is: " << month << "-0" << day << "-" << year;
	else
		cout << "The date you entered is: " << month << "-" << day << "-" << year;
	cout << endl;

}

void date::printLongDate() const
{
	cout << "You have entered ";
	if (month <= 1)
		cout << "January " << day << ", " << year;
	else if (month <= 2)
		cout << "February " << day << ", " << year;
	else if (month == 3)
		cout << "March " << day << ", " << year;
	else if (month == 4)
		cout << "April " << day << ", " << year;
	else if (month == 5)
		cout << "May " << day << ", " << year;
	else if (month == 6)
		cout << "June " << day << ", " << year;
	else if (month == 7)
		cout << "July " << day << ", " << year;
	else if (month == 8)
		cout << "August " << day << ", " << year;
	else if (month == 9)
		cout << "September " << day << ", " << year;
	else if (month == 10)
		cout << "October " << day << ", " << year;
	else if (month == 11)
		cout << "November " << day << ", " << year;
	else
		cout << "December " << day << ", " << year;
	cout << endl;
}

//Constructor with parameters
date::date(int m, int d, int y)
{
	month = m;
	day = d;
	year = y;
}


Hope I helped,

Zachary Law
Last edited on
Hey Zachary, thanks for looking into this. If I run that now as is, with your mods, I receive an invalid date regardless of the month I input.

I am going to add your suggested {} and see what else I can do. Thanks again for checking this out.
You might want to put your boolean return value back in your date validation function (I don't want to break your program ;D). I just changed that as an example. I think you had a logic error somewhere. Your date validation function would return false,... then after it would return false.. nothing else would happen because you never told it to print out a statement like: "Invalid input"
1
2
3
4
5
	enum eMonths {Jan, Feb, Mar, Apr, May, June, July, Aug, Sept, Oct, Nov, Dec};
	string Months[] = {"", "January", "February", "March", "April", "May", "June",
				"July", "August", "September", "October", "November", "December"};
	
	cout << Months[eMonths::Jan] << endl;


January
Last edited on
Topic archived. No new replies allowed.