#include #include #include //for class system_clock #include //for the function localtime using namespace std; void travel(int& hour, int& minute, int& second, int distance); int main() { const auto now {chrono::system_clock::now()}; const time_t t {chrono::system_clock::to_time_t(now)}; const tm *const p {localtime(&t)}; //Get the current hour, minute, and second. int h {p->tm_hour}; //0–23 int m {p->tm_min}; //0–59 int s {p->tm_sec}; //0–59 cout << "How many seconds forward do you want to go from " << h << ":" << m << ":" << s << "? "; 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 h, m, s, //but not the value of distance. travel(h, m, s, distance); cout << "The new time is " << h << ":" << m << ":" << s << ".\n"; return EXIT_SUCCESS; } //Move the time distance seconds into the future. void travel(int& hour, int& minute, int& second, int distance) { /* Walk distance seconds into the future. Each iteration advances 1 second. */ for (int i {0}; i < distance; ++i) { if (second < 59) { ++second; } else { second = 0; if (minute < 59) { ++minute; } else { minute = 0; if (hour < 23) { ++hour; } else { hour = 0; //Advance into a new day (but not tracking days) } } } } }