Question: #include using namespace std; bool isLeap(int year) { return (((year % 4) == 0) && (((year % 100) != 0) || ((year % 400) ==
#include
using namespace std;
bool isLeap(int year) { return (((year % 4) == 0) && (((year % 100) != 0) || ((year % 400) == 0))); }
int main() { int day, month, year, dayNumber;
cout<< "Please enter the month: "; cin>> month; cout<<"Please enter the day: "; cin>> day; cout<<"Please enter the year :"; cin>> year;
int daysPerMonth[] = {31, (isLeap(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int num_of_days = 0; // Sum the number of days for all months preceding the current month for(int i = 0; i < (month - 1); ++i) num_of_days += daysPerMonth[i];
// Finally just add the day entered for the current month num_of_days += day;
cout<<"Date: "< return 0; } Topic Branching switch Statement Description Enhance the last assignment (program above) to include input validation. The enhanced assignment should check the values of day, month, and year entered by the user to ensure that they are OK. If they are not OK, the program should display an error message and return. The program should check the input values before computing the day number. The program should validate the input values to ensure that: Year value is from 2000 to 2099 inclusive. Month value is from 1 to 12 inclusive. Day value is valid for the given month and the year. Testing Input Test Run 1 Enter Month: 4 Enter Day: 31 Enter Year: 2003 Output Test Run 1 Mismatching Month/Day Values Entered. Month Value Entered: 4 Day Value Entered: 31 Sample Code /* Java Code */ int day, month, year; //input day, month, and year values here //Validating Year Value if ( year < 2000 || year > 2099 ) { JOptionPane.showMessageDialog (null, Unacceptable Year Entered: + year ); JOptionPane.showMessageDialog (null, Acceptable range For Year is 2000 to 2099 ); System.return (0); } //Determining Leap year boolean leapyear = false; if ((year % 4) == 0) { leapyear = true; } //Validating Month Value if ( month < 1 || month > 12 ) { JOptionPane.showMessageDialog (null, Invalid Month Entered: + month ); System.return (0); } //Validating YearValue if ( year < 2000 || year > 2099 ) { JOptionPane.showMessageDialog (null, Unacceptable Year Entered: + year ); JOptionPane.showMessageDialog (null, Valid Range For Year 2000 to 2099 ); System.return (0); } //Validating Day Value if ( ( day < 1 ) || ( day > 31 ) ) { JOptionPane.showMessageDialog (null, Invalid Day Entered: + day ); System.return (0); } //Validating Other Day Values if ( ( month == 4 || month == 6 || month == 9 || month == 11) && ( day > 30 ) ) { JOptionPane.showMessageDialog (null, Mismatching Month/Day Values Entered. ); JOptionPane.showMessageDialog (null, Month Value Entered: + month ); JOptionPane.showMessageDialog (null, Day Value Entered: + day ); System.return (0); } if ( (month == 2) && ( day > 29 ) && ( leapyear ) ) { JOptionPane.showMessageDialog (null, Mismatching Month/Day Values Entered: ); JOptionPane.showMessageDialog (null, Month Value Entered: + month ); JOptionPane.showMessageDialog (null, Day Value Entered: + day ); System.return (0); } if ( (month == 2) && ( day > 28 ) && ( ! leapyear ) ) { JOptionPane.showMessageDialog (null, Mismatching Month/Day Values Entered: ); JOptionPane.showMessageDialog (null, Month Value Entered: + month ); JOptionPane.showMessageDialog (null, Day Value Entered: + day ); System.return (0); } /* C++ Code */ /* The code below asks the user to input month, day and year one after the other. It then validates the values entered. If any of the values is invalid, it displays an error message and returns. */ int day, month, year; //input day, month, and year values here. //Validate input values //Determining Leap year Bool leapyear = false; if ((year % 4) == 0) { leapyear = true; } //Validating YearValue if ( year < 2000 || year > 2099 ) { cout << Unacceptable Year Entered: << year << endl; cout << Valid Range For Year 2000 to 2099 << endl; return (0); } //Validating Month Value if ( month < 1 || month > 12 ) { cout << Invalid Month Entered: << month << endl; return (0); } //Validating Day Value if ( ( day < 1 ) || ( day > 31 ) ) { cout << Invalid Day Entered: << day << endl; return (0); } //Validating Other Day Values if ( ( month == 4 || month == 6 || month == 9 || month == 11) && ( day > 30 ) ) { cout << Mismatching Month/Day Values Entered. << endl; cout << Month Value Entered: << month << endl; cout << Day Value Entered: << day << endl; return (0); } if ( (month == 2) && ( day > 29 ) && ( leapyear ) ) { cout << Mismatching Month/Day Values Entered: << endl; cout << Month Value Entered: << month << endl; cout << Day Value Entered: << day << endl; return (0); } if ( (month == 2) && ( day > 28 ) && ( ! leapyear ) ) { cout << Mismatching Month/Day Values Entered: << endl; cout << Month Value Entered: << month << endl; cout << Day Value Entered: << day << endl; return (0); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
