#include #include #include using namespace std; // This function CHANGES the values of h, m, s that are passed to it. // It moves the time forward by 'delta' seconds. void travel(int &h, int &m, int &s, int delta) { const int SECONDS_PER_DAY = 24 * 60 * 60; // 86400 // Convert current time to total seconds since midnight int total = h * 3600 + m * 60 + s; // Move forward by delta seconds total += delta; // Wrap around within a 24-hour day total %= SECONDS_PER_DAY; if (total < 0) { total += SECONDS_PER_DAY; } // Convert back to hours, minutes, seconds h = total / 3600; int leftover = total % 3600; m = leftover / 60; s = leftover % 60; } // Print time in 12-hour format with a.m./p.m. void print_time_12h(int h, int m, int s) { bool pm = (h >= 12); int display_h = h % 12; if (display_h == 0) { display_h = 12; } cout << display_h << ':' << setw(2) << setfill('0') << m << ':' << setw(2) << setfill('0') << s << ' ' << (pm ? "p.m." : "a.m."); } int main() { // Starting time: 6:00:00 p.m. = 18:00:00 in 24-hour time int hour {18}; int minute {0}; int second {0}; cout << "How many seconds forward do you want to go from 6:00:00 p.m.? "; int distance {}; if (!(cin >> distance)) { cerr << "That wasn't a valid number of seconds.\n"; return EXIT_FAILURE; } // This call will CHANGE hour, minute, second travel(hour, minute, second, distance); cout << "The new time is "; print_time_12h(hour, minute, second); cout << '\n'; return EXIT_SUCCESS; }