#include #include #include //for time_t, time, tm, localtime using namespace std; void travel(int& month, int& day, int& year, int distance); int main() { //Get the current year, month, and day of the month. time_t t {time(nullptr)}; tm *p {localtime(&t)}; int y {p->tm_year + 1900}; int m {p->tm_mon + 1}; //in the range 1 to 12 inclusive int d {p->tm_mday}; //in the range 1 to 31 inclusive cout << "How many days forward do you want to go from " << m << "/" << d << "/" << y << "? "; int distance {0}; cin >> distance; if (!cin) { cerr << "Sorry, that wasn't a number.\n"; return EXIT_FAILURE; } if (distance < 0) { cerr << "Distance " << distance << " can't be negative.\n"; return EXIT_FAILURE; } //The following statement can change the value of m, d, y, //but not the value of distance. travel(m, d, y, distance); cout << "The new date is " << m << "/" << d << "/" << y << ".\n"; return EXIT_SUCCESS; } //Move the date distance days into the future. void travel(int& month, int& day, int& year, int distance) { static int length[] { //no need to recreate this array with every call 0, //dummy, so that January will have subscript 1 31, //January 28, //February, assuming there are no leap years 31, //March 30, //April 31, //May 30, //June 31, //July 31, //August 30, //September 31, //October 30, //November 31 //December }; static int n {size(length) - 1}; //#of months in year,not counting dummy //Walk distance days into the future. //Each iteration of the loop advances 1 day into the future. //Each ++ will now change the value of an actual argument up in main. for (int i {0}; i < distance; ++i) { if (day < length[month]) { ++day; } else { day = 1; //Advance into a new month. if (month < n) { ++month; } else { month = 1; //Advance into a new year. ++year; } } } }