Calculating the difference of two times in C++ -
one of problems i'm working on requires input of 2 time values (being in hours, minutes , seconds)in 24h00 format. have declared variables, input , output statements regards main program. i'm having trouble void statement calcdiff calculate difference of 2 time inputs. values have been declared of type int. should inputs(eg. sec , sec2) compared first see greater before calculating difference? i'm assuming order of variables important too(calculating difference of hours before minutes, before seconds)? i'm new c++ apologies if i'm missing obvious.
// computes time difference of 2 time periods in 24h00 format // time periods input #include <iostream> using namespace std; int seconds,seconds2, minutes, minutes2, hours, hours2; void calcdiff(int seconds, int seconds2 int minutes, int minutes2, int hours, int hours2); int main() { int sec, sec2, min, min2, hr, hr2, diff; cout << "enter first time." << endl; cout << "enter hours, minutes , seconds: "; cin >> hr >> min >> sec; cout << "enter second time." << endl; cout << "enter hours, minutes , seconds: "; cin >> hr2 >> min2 >> sec2; calcdiff(int sec, int sec2, int min, int min2, int hr, int hr2, diff); cout << endl << "difference in times: " << hr << ":" << min << ":" << sec; cout << " - " << hr2 << ":" << min2 << ":" << sec2; return 0; } void calcdiff(int seconds, int seconds2, int minutes, int minutes2, int hour, int hour2, diff)
use <chrono> plus an existing library.
#include "date.h" #include <iostream> int main() { std::chrono::seconds t1, t2; std::cout << "enter first time [h]h:mm:ss: "; std::cin >> date::parse("%t", t1); if (std::cin.fail()) return 1; std::cout << "enter second time [h]h:mm:ss: "; std::cin >> date::parse(" %t", t2); if (std::cin.fail()) return 1; std::cout << date::format("difference in times: %t\n", t1 - t2); } the above library free, open-source, , being proposed standardization.
Comments
Post a Comment