Day Number of a date
#include
using namespace std;
//PURPOSE: Calculate the number of days in February for a given year
//PRECONDITION: Check whether the year is a leap or not
//POSTCONDTION: return the correct number of days in February for that year
int febdays(int year)
{
if(year%100 == 0 && year%400==0){
return 29;
}
else if(year%4 == 0){
return 29;
}
else{
return 28;
}
}
//PURPOSE: Calculate the number of days for a given date within that year
//PRECONDITION: Check whether the year is a leap or not
//POSTCONDTION: return the corresponding number of days for that year
int daynum(int month, int day, int year)
{
int yeardays = 0;//declare and initialize variable to hold the day number of the year
if(month == 1){
yeardays = day;
return yeardays;
}
if(month == 2){
yeardays = 31 + day;
return yeardays;
}
if(month == 3){
yeardays = 31 + febdays(year) + day ;
return yeardays;
}
if(month == 4){
yeardays = (31 * 2) + febdays(year) + day;
return yeardays;
}
if(month == 5){
yeardays = (31 * 2) + 30 + febdays(year) + day;
return yeardays;
}
if(month == 6){
yeardays = (31 * 3) + 30 + febdays(year) + day;
return yeardays;
}
if(month == 7){
yeardays = (31 * 3) + (30 * 2) + febdays(year) + day;
return yeardays;
}
if(month == 8){
yeardays = (31 * 4) + (30 * 2) + febdays(year) + day;
return yeardays;
}
if(month == 9){
yeardays = (31 * 5) + (30 * 2) + febdays(year) + day;
return yeardays;
}
if(month == 10){
yeardays = (31 * 5) + (30 * 3) + febdays(year) + day;
return yeardays;
}
if(month == 11){
yeardays = (31 * 6) + (30 * 3) + febdays(year) + day;
return yeardays;
}
if(month == 12){
yeardays = (31 * 6) + (30 * 4) + febdays(year) + day;
return yeardays;
}
return 0;
}
//PURPOSE: Check whether the month entered by the user is valid
//PRECONDITION: Month should be between 1 and 12
//POSTCONDTION: Display error message if the month is invalid
int CheckMonth(int month)
{
if(month <> 12){
cout << ">> Invalid Month <<\n";
cout << "Month should be between 1 and 12 \n";
return 1;
}
return 0;
}
//PURPOSE: Check whether the day entered by the user is valid
//PRECONDITION: Day should be in agreement with the month. e.g September has 30 days not 31
//POSTCONDTION: Display error message if the day is invalid
int CheckDay(int month, int day)
{
if(day <>
cout << ">>Invalid Day<<\n";
cout << "Day has to be a positive number\n";
return 1;
}
if(month == 2){
if(day > 29){
cout << ">> Invalid Day <<\n";
cout << "February has 28 days in a regular year and 29 days in a leap year\n";
return 1;
}
}
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
if(day > 31){
cout << ">> Invalid Day <<\n";
cout << "Day should be between 1 and 31 \n";
return 1;
}
}
else{
if(day > 30){
cout << ">> Invalid Day <<\n";
cout << "Day should be between 1 and 30\n";
return 1;
}
}
return 0;
}
//PURPOSE: Check whether the year entered by the user is valid
//PRECONDITION: Year should be greater than zero
//POSTCONDTION: Display error message if the year is invalid
int CheckYear(int day, int year)
{
if(year <>
cout << ">> Invalid Year <<\n";
cout << "Year has to be positive\n";
return 1;
}
if(day == 29 && febdays(year)!=29){
cout << ">> Invalid Year <<\n";
cout <<>
cout << "Therefore Februaury cannot have 29 days\n";
return 1;
}
return 0;
}
//PURPOSE: Prompt user for the date in month/day/year format
//PRECONDITION: Check whether the entered date is valid, display error messages if it is not correct
//POSTCONDTION: return 0 to indicate success
int main()
{
int month = 0, day = 0, year = 0;
cout << "Enter the month: ";
cin >> month;
if(CheckMonth(month) == 1){
return 0;
}
cout << "Enter the day: ";
cin >> day;
if (CheckDay(month, day) == 1){
return 0;
}
cout << "Enter the year: ";
cin >> year;
if (CheckYear(day,year) == 1){
return 0;
}
cout <<>
return 0;
}