#include #include #include //for time_t, time, tm, localtime using namespace std; int main() { int length[][13] { //dum Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, //non-leap {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} //leap }; int n {12}; //number of months in a year //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) { int leap {0}; //will be used as a subscript if (year % 400 == 0 || year % 100 != 0 && year % 4 == 0) { leap = 1; //year is a leap year } else { leap = 0; //year is not a leap year } if (day < length[leap][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; }