How can I check for an invalid date when I run the program? I am trying to enter a conditional statement that checks for a valid or invalid date.
Programing Question: Write a program that inputs a date (e.g., July 4, 2008) and outputs the day of the week that corresponds to that date. Here is what I Have. The program runs. I need it to loop around.
#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, 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.";
}
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;
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;
}
}
elseif (month == MARCH)
{
result = 3;
}
elseif (month == APRIL)
{
result = 6;
}
elseif (month == MAY)
{
result = 1;
}
elseif (month == JUNE)
{
result = 4;
}
elseif (month == JULY)
{
result = 6;
}
elseif (month == AUGUST)
{
result = 2;
}
elseif (month == SEPTEMBER)
{
result = 5;
}
elseif (month == OCTOBER)
{
result = 0;
}
elseif (month == NOVEMBER)
{
result = 3;
}
elseif (month == DECEMBER)
{
result = 5;
}
return result;
}
For validating days and months just check if they have values from 1 to 12 for the month and after that, depending on which month it is check if the day is 1-31, 1-30 or special case for february 1-28 / 1-29 which also depend from the year.
You mentioned putting the program into a loop...Just put all of your code in "main" in a "while(1)" loop and make sure you have a condition for exiting the while loop and quiting the program.
Tip: it's much better to use switch than ifelse when comparing same number to a lot of constant values.