I need some help. How to make a check invalid month, days and leap year.

#include <iostream>


using namespace std;

const int JANUARY = 1;
const int FEBRUARY = 2;
const int MARCH = 3;
const int APRIL = 4;
const int MAY = 5;
const int JUNE = 6;
const int JULY = 7;
const int AUGUST = 8;
const int SEPTEMBER = 9;
const int OCTOBER = 10;
const int NOVEMBER = 11;
const int DECEMBER = 12;

// Returns true if the given year is a leap year
bool is_leap_year(int year);
// Returns a value computed from the century of the year
int get_century_value(int year);
// Returns a value computed based on the years since the beginning of the century.
int get_year_value(int year);
// Returns a value (from a table) for the given month
int get_month_value(int month, int year);

int main()

{



int month, day, year, day_of_week;


cout << " Please enter a month: ";
cin >> month;
cout << endl;
cout << " Please enter a day: ";
cin >> day;
cout << endl;
cout << " Please enter a year: ";
cin >> year;



// Compute the day of the week
day_of_week = day + get_month_value(month, year) + get_year_value(year) + get_century_value(year);

day_of_week = day_of_week % 7;

cout << "\n The date " << month << "/" << day << "/" << year
<< " is a ";

if (day_of_week == 0)
{
cout << "Sunday.";
}
else if (day_of_week == 1)
{
cout << "Monday.";
}
else if (day_of_week == 2)
{
cout << "Tuesday.";
}
else if (day_of_week == 3)
{
cout << "Wednesday.";
}
else if (day_of_week == 4)
{
cout << "Thursday.";
}
else if (day_of_week == 5)
{
cout << "Friday.";
}
else if (day_of_week == 6)
{
cout << "Saturday.";
}
cout << endl;
return 0;
}


// Prompts the user to input a month, day and year
void get_input(int month, int day, int year)

{
cout << endl;
cout << " Please enter a month: ";
cin >> month;
cout << endl;
cout << " Please enter a day: ";
cin >> day;
cout << endl;
cout << " Please enter a year: ";
cout << endl;
cin >> year;
}

// Returns true if the given year is a leap year

bool is_leap_year(int year)

{
return (((year % 400) == 0) ||(((year % 4) == 0)
&& ((year % 100) != 0)));

}

// Returns a value computed from the century of the year

int get_century_value(int year)

{
int century;
int remainder;
century = year/100;
remainder = (century % 4);
return ((3 - remainder) * 2);
}

// Returns a value computed based on the years since
// the beginning of the century.
int get_year_value(int year)

{
int sinceCentury;
sinceCentury = year % 100;
return (sinceCentury + (sinceCentury/4));
}

// Returns a value (from a table) for the given month

int get_month_value(int month, int year)
{
int result;
if (month == JANUARY)
{
if (is_leap_year(year))
{
result = 6;
}
else
{
result = 0;
}
}
if (month == FEBRUARY)
{
if (is_leap_year(year))
{
result = 2;
}
else
{
result = 3;
}

}
else if (month == MARCH)

{
result = 3;
}

else if (month == APRIL)

{
result = 6;
}

else if (month == MAY)

{
result = 1;
}

else if (month == JUNE)

{
result = 4;
}

else if (month == JULY)

{
result = 6;
}

else if (month == AUGUST)

{
result = 2;
}

else if (month == SEPTEMBER)

{
result = 5;
}

else if (month == OCTOBER)

{
result = 0;
}

else if (month == NOVEMBER)

{
result = 3;
}

else if (month == DECEMBER)

{
result = 5;
}

return result;

}
Last edited on
there is some day-month-year input validation code here but the leap year validation code is not entirely correct, you'll have to sort that one out since years like 1900, 1700 etc are not leap years:
http://stackoverflow.com/questions/36229110/c-month-day-and-year-validation
you should be able to find other leap year validation code elsewhere and put it together with above
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
constexpr int CALENDAR_START = 1582 ; // gregorian calendar

enum { JANUARY = 1, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY,
       AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER };
static_assert( DECEMBER == 12, "error in initialising constants for months" ) ; // sanity check

bool is_leap( int year ) { return ( ( year%4 == 0 && year%100 != 0 ) || year%400 == 0 ) ; }

int days_in_month( int month, int year )
{
    switch(month)
    {
        case JANUARY: case MARCH: case MAY: case JULY: case AUGUST: case OCTOBER: case DECEMBER:
            return 31 ;

        case APRIL: case JUNE: case SEPTEMBER: case NOVEMBER:
            return 30 ;

        case FEBRUARY: return is_leap(year) ? 29 : 28 ;

        default: return 0 ; // error: invalid_argument
    }
}

bool is_valid_date( int day, int month, int year )
{
    return year > CALENDAR_START && // valid year
           month >= JANUARY && month <= DECEMBER && // valid month
           day > 0 && day <= days_in_month( month, year ) ; // valid day
}
This is not the whole code right ?
And constexpr doesnt work on code blocks.
Last edited on
> This is not the whole code right ?

No. It is just a part of the code - the (untested) code which checks if the three integers day, month and year constitute a valid date in the gregorian calendar.


> And constexpr doesnt work on code blocks.

Enable C++11.
See: http://www.cplusplus.com/doc/tutorial/introduction/codeblocks/
And though the article does not suggest it (it should have), also check -Wall -Wextra and -pedantic-errors

Or change the constexpr to const and comment out the line with the static_assert.
Last edited on
Is there a possibility to send me the whole code so I can see the difference cause I am just a beginner.
The only changes I made to your original code are:

a. placed JANUARY, FEBRUARY etc. in an anonymous enum;
you can instead have the twelve named constants as in the original code; it would work just the same.

b. The function bool is_leap( int year ) - I wrote it just to make the validation code self-contained;
you can instead use your own bool is_leap_year(int year)

The rest are two functions which are additions:
int days_in_month( int month, int year ) which returns the number of days in the month
and bool is_valid_date( int day, int month, int year ) which returns true if we have a valid date.
Just use an enum to ensure right uses of months. It is recommended to make sure you know all kind of techniques while coding as if you don't it can make your programming less good. It can also protect you from making mistakes and result with less errors. Of course you can always use some program such as checkmarx and other to detect your errors if you'd like.
Anyway, good luck!
Ben.
Topic archived. No new replies allowed.