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 ";
// 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;
}
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
constexprint 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
}
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.
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.