// // Program to test "next_time" function. // #include #include #include // has setw(), setfill() // "error_exit" function. // Post: "Bad input" printed; program exited. // (Yes, this is extremely crude.) void error_exit(void) { cout << "Bad input\n"; exit(EXIT_FAILURE); } // "next_time" function. // Pre: "hours", "minutes", "seconds" represent a time in 24-hour // (military) format, e.g. 14:00:01. // Post: Prints the next time (1 second later). void next_time(int hours, int minutes, int seconds) { // check for acceptable values. if ((hours < 0) || (hours > 23)) error_exit(); if ((minutes < 0) || (minutes > 59)) error_exit(); if ((seconds < 0) || (seconds > 59)) error_exit(); // compute next second. seconds = seconds + 1; // next minute? if (seconds > 59) { minutes = minutes + 1; seconds = 0; } // next hour? if (minutes > 59) { hours = hours + 1; minutes = 0; } // next day? if (hours > 23) { hours = 0; } // print result. cout << "next time: " << setw(2) << setfill('0') << hours << ":" << setw(2) << setfill('0') << minutes << ":" << setw(2) << setfill('0') << seconds << endl; return; } int main(void) { int h, m, s; cout << "Enter integers for hours, minutes, seconds:\n"; cin >> h >> m >> s; next_time(h, m, s); return EXIT_SUCCESS; }