I need help with my program. My program is supposed to get ask the user a date (MM DD YYYY) and its supposed to tell them what day of the week that specific date is. Well, I was told to check for leap years, and if the Feb 29 is a non-leap year, its supposed to cout an invalid statement and tells the user to try again. My problem is that its not looping through only when its Feb 29 2015 for example. Can someone help me fix this. I have been trying to find a solution and I cant find one so far? Like, I said, it works for other dates, just not checking and looping Feb 29 2014 or 2015, etc?
#include <iostream>
usingnamespace std;
constint JANUARY = 1;
constint FEBRUARY = 2;
constint MARCH = 3;
constint APRIL = 4;
constint MAY = 5;
constint JUNE = 6;
constint JULY = 7;
constint AUGUST = 8;
constint SEPTEMBER = 9;
constint OCTOBER = 10;
constint NOVEMBER = 11;
constint 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 =0;
int day = 0;
int year = 0;
int day_of_week = 0;
int days_in_month = 0;
bool dates_invalid=true;
do
{
cout << endl;
cout << " Please enter a integer (between 1 and 12) corresponding " << endl;
cout << endl;
cout << " to the appropriate month (January to December): ";
cin >> month;
cout << endl;
cout << endl;
cout << " Please enter a integer (between 1 and 31) corresponding" << endl;
cout << endl;
cout << " to the number of days in that particular month: ";
cin >> day;
cout << endl;
cout << endl;
cout << " Please enter a year (between 2000 and 2020): ";
cin >> year;
cout << endl;
cout << endl;
//check for leap year
if(month == 9 || month == 4 || month == 6 || month == 11)
{
days_in_month = 30;
}
elseif(month == 2)
{
if( (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
days_in_month = 29;
}
else
{
days_in_month = 28;
}
}
else
{
days_in_month = 31;
}
if( month < 1 || month > 12 || day <= 0 || day > days_in_month)
{
cout << endl;
cout << " You have entered an invalid date. Please try again." << endl;
dates_invalid=false;
cout << endl;
cout << endl;
}
if( year < 2000 || year > 2020)
{
cout << endl;
cout << " You have entered an invalid date. Please try again." << endl;
dates_invalid=false;
cout << endl;
cout << endl;
}
else
dates_invalid = true;
}
while (dates_invalid==false);
// 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 << endl;
cout << endl;
cout << "\n The date " << month << "/" << day << "/" << year
<< " is a ";
if (day_of_week == 0)
{
cout << "Sunday.";
}
elseif (day_of_week == 1)
{
cout << "Monday.";
}
elseif (day_of_week == 2)
{
cout << "Tuesday.";
}
elseif (day_of_week == 3)
{
cout << "Wednesday.";
}
elseif (day_of_week == 4)
{
cout << "Thursday.";
}
elseif (day_of_week == 5)
{
cout << "Friday.";
}
elseif (day_of_week == 6)
{
cout << "Saturday.";
}
cout << endl;
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)));
}
Wouldn't it make more sense just to loop it over to March 1st if it isn't a leap year? Or for any access to a day that is longer than the month- just go up to the next one.
Also, line 69's if statement should just use is_leap_year for brevity.