Valid Date issues

Okay, so now I have a new problem. The Gregorian date checker works with real dates, but I need to also use a function isValidDate to make sure that no incorrect date inputs are used. Again, it works with true dates, but when I put in dates that don't exist (as you'll see from the tests I'll provide,) the function outputs true as a valid date. The function also calls two other functions, isGregorianDate, and isLeapYear, which both work properly. It looks kind of messy, but the following are the functions, and below them I'll put my tests for isValidDate.

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
bool isLeapYear(int year) {
    return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
}

bool isGregorianDate(int month, int day, int year) {
    {
        if (year < 1752)
            return false;

        if ((year == 1752) && (month < 9))
            return false;

        if ((month == 9) && (day < 13))
            return false;

        return true;
    }
}

bool isValidDate(int month, int day, int year) {
    if (month > 12) {
        return false;
    }
    if (month < 1) {
        return false;
    }
    if (day < 1)  {
        return false;
    }
    if (day > 31) {
        return false;
    }
    if (((isGregorianDate) && ((month = 1 || 3 || 5 || 7 || 8 || 10 || 12)
        && ((day >= 1) && (day <= 31)))) || ((isGregorianDate) && ((month =
        4 || 6 || 9 || 11) && ((day >= 1) && (day <= 30)))) || ((isGregorianDate) &&
        ((month = 2) && ((day >= 1) && (day <= 28)))) || ((isGregorianDate) &&
        (isLeapYear) && ((month = 2) && ((day >= 1) && (day <= 29))))) {
        return true;
    }
}

static void test_isValidDate() {
    cout << "Testing -- isValidDate()" << endl;

    cout << isGregorianDate(8, 19, 2016) << " PROPER INPUT---correct value is: 1" << endl;
    cout << isGregorianDate(3, 12, 1234) << " PROPER INPUT---correct value is: 0" << endl;
    cout << isGregorianDate(1, 1, 2000000) << " PROPER INPUT---correct value is: 1" << endl;
    cout << isGregorianDate(1, 1, 1) << " PROPER INPUT---correct value is: 0" << endl;
    cout << isGregorianDate(1, 1, -10) << " PROPER INPUT---correct value is: 0" << endl;
    cout << isGregorianDate(1, 1, -2000000) << " PROPER INPUT---correct value is: 0" << endl;
    cout << isGregorianDate(13, 1, 2000) << " IMPROPER INPUT---correct value is: 0" << endl;
    cout << isGregorianDate(1, 40, 2000) << " IMPROPER INPUT---correct value is: 0" << endl;
    cout << isGregorianDate(-1, 1, 2000) << " IMPROPER INPUT---correct value is: 0" << endl;
    cout << isGregorianDate(1, -1, 2000) << " IMPROPER INPUT---correct value is: 0" << endl;

    cout << "Finished testing -- isValidDate()" << endl << endl;
}


Last edited on
closed account (48T7M4Gy)
The 'usual' way to validate dates is to write down the business rules and THEN and ONLY then write the code corresponding to those business rules.

Often that is done by using several arrays (or <vectors> - one showing the months ( 1-12 ) and another showing the corresponding days in a month in a parallel array. Leap years need to be addressed as another business rule. Who knows what the G-calendar does with that?

(Maybe consider he use of structs or classes, but that is not mandatory.)

The sort of question that arises and needs addressing in the code is whether month[x] has 1 <= no_days <= month[x] where month[x] is the no of days in month x, given that the month and year have already been tested against the cutoff dates.



Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool is_valid_date(int aDayNo, int aMonth, int aYear)
{
    int days_in_month[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    
    if( aYear % 4 == 0 )
        days_in_month[2] = 29;
    
    if ( aMonth < 1 or aMonth > 12 )
        return false;
    
    if ( aDayNo < 1 or aDay > days_in_month[aMonth] )
        return false;
    
    return true;
}
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/207530/
http://www.cplusplus.com/forum/beginner/207463/
Last edited on
Topic archived. No new replies allowed.