#include #include #include //for time_t, time, tm, localtime using namespace std; int main() { int length[] { 0, //dummy element so that January will have subscript 1 31, //January 28, //February 31, //March 30, //April 31, //May 30, //June 31, //July 31, //August 30, //September 31, //October 30, //November 31 //December }; int n {size(length) - 1}; //the number of months, not counting the dummy //Get the current year, month, and day of the month. time_t t {time(nullptr)}; tm *p {localtime(&t)}; int year {p->tm_year + 1900}; int month {p->tm_mon + 1}; //in the range 1 to 12 inclusive int day {p->tm_mday}; //in the range 1 to 31 inclusive cout << "How many days forward do you want to go from " << month << "/" << day << "/" << year << "? "; 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; } //Walk distance days into the future. //Each iteration of the loop advances 1 day into the future. 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; } } } cout << "The new date is " << month << "/" << day << "/" << year << ".\n"; return EXIT_SUCCESS; }