#include // A date is stored using three variables: // // 'year', 'month' and 'day'. // // Write a program that asks the user to enter another // date and determines whether the entered date is before, // the same as, or after the stored date. // // For the purposes of this exercise, assume that every // month has 31 days. // // If the user enters // // - a year equal to 0, // - a month not between 1 and 12, or // - a day not between 1 and 31, // // terminate the program by returning 0 from main(). // // Otherwise, print // // past // // if the entered date is before the stored date, // // same day // // if the two dates are the same, and // // future // // if the entered date is after the stored date. // // Notice that negative years are permitted. As in an // earlier exercise, negative and positive integers may // be used to represent years before and after the // beginning of an era, with no year 0. // Function declarations int main(); // Function definitions int main() { int year{ 2022 }; int month{ 9 }; int day{ 13 }; int user_year{}; int user_month{}; int user_day{}; std::cout << "past" << std::endl; return 0; } // Hint: // // Compare the years first. // // If the entered year is less than the stored year, you // already know that the entered date is in the past. // There is no reason to examine the month or day. // // Similarly, if the entered year is greater than the // stored year, you already know that the entered date is // in the future. // // Only if the years are equal do you need to compare the // months. // // Similarly, only if both the years and months are equal // do you need to compare the days. // // Thus, the comparison naturally has a nested structure. // Here is one solution: /* int main() { int year{ 2022 }; int month{ 9 }; int day{ 13 }; int user_year{}; std::cout << "Enter a year: "; std::cin >> user_year; if ( user_year == 0 ) { return 0; } int user_month{}; std::cout << "Enter a month (1-12): "; std::cin >> user_month; if ( (user_month < 1) || (user_month > 12) ) { return 0; } int user_day{}; std::cout << "Enter a day (1-31): "; std::cin >> user_day; if ( (user_day < 1) || (user_day > 31) ) { return 0; } if ( user_year < year ) { std::cout << "past" << std::endl; } else if ( user_year > year ) { std::cout << "future" << std::endl; } else { // The years are equal, so compare the months. if ( user_month < month ) { std::cout << "past" << std::endl; } else if ( user_month > month ) { std::cout << "future" << std::endl; } else { // The years and months are equal, so compare // the days. if ( user_day < day ) { std::cout << "past" << std::endl; } else if ( user_day > day ) { std::cout << "future" << std::endl; } else { std::cout << "same day" << std::endl; } } } return 0; } */ // It is also possible to write the comparison using // compound logical expressions. For example, an entered // date is in the past if // // user_year < year // // OR the years are equal and // // user_month < month // // OR the years and months are equal and // // user_day < day. // // This can be written as: // // if ( (user_year < year) // || ((user_year == year) && (user_month < month)) // || ((user_year == year) && (user_month == month) // && (user_day < day)) ) { // std::cout << "past" << std::endl; // } // // The expression is correct, but compare it with the // nested solution above. // // Which version makes the process of comparing dates // easier to follow?