^.^ something easy...

Im trying to limit the months in a year that only have 30 days to only give feedback for 30 days. Anything over that such as 4 31 2000 would give you a 'Please enter valid date.' message. same goes for the 31 day months. Feburary as well. Im not seeing something....I know im close though....
Ive been on this too long...I would love any feed back. Thanks!


cout << "Please enter date. (Month Day Year)" << endl
<< "ex. xx xx xxxx " << endl;
cin >> m >> d >> y;

if ( m == 1, 3, 5, 7, 8, 10, 12 && d > 31 || d < 1 )
{ cout << "Please enter a vaid date." << endl;
cin >> j; }

//Fixed my problem, was line below this one. ***************
looks like I couldnt chain my variable m as a comparative equal to. Everything else works perfectly. Thanks anyway. ****************

/* else if ( m == 4, 6, 9, 11 && d > 30 || d < 1)
{ cout << "Please enter a vaid date." << endl;
cin >> j; }*/

Fixed to .....

else if ( m == 4 || m == 6 || m == 9 || m == 11 && d > 30 || d < 1)
{ cout << "Please enter a vaid date." << endl;
cin >> j; }

else if ( m == 2 && d == 29 && y % 4 != 0 )
{cout << "Please enter a vaid date." << endl;
cin >> j; }
Last edited on
As example:
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
#include <iostream>

bool validate_date(int year, int month, int day)
{
    static const int days_in_month[12] = {31, 0/*special case*/, 31, 30, 31, 30,
                                          31, 31, 30, 31, 30, 31};
    if (month > 12 || month < 1 || day < 1)
        return false;
    else if (month != 2)
        return (days_in_month[month - 1] >= day);
    else {//february case
        //do it yourself.
        //Note that it is NOT simle year % 4 == 0 check. It is more complex.
        //Read wiki if you are not sure when does leap year happens
    }
    return true;
}

int main()
{
    using namespace std;
    int m,d,y;
    cout << "Please enter date. (Month Day Year)" << endl
         << "ex. xx xx xxxx " << endl;
    cin >> m >> d >> y;

    if (!validate_date(y,m,d)) {
        cout << "Please enter a vaid date." << endl;
    }
    return 0;
}

Edit: minor logic error.
Last edited on
Topic archived. No new replies allowed.