Program to check Time Difference in CPP (C plus plus)
In this tutorial, we will learn about the Program to check Time Difference in CPP (C plus plus).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#include <iostream> using namespace std; struct time { int hrs; int mins; int secs; }; void computeTimeDifference(struct time, struct time, struct time *); int main() { struct time t1, t2, difference; cout << "Please enter Starting Time: " << endl; cout << "Please enter Hours, Minutes & Seconds respectively: "; cin >> t1.hrs >> t1.mins >> t1.secs; cout <<endl<< "Please enter Stop Time: " << endl; cout << "Please enter Hours, Minutes & Seconds respectively: "; cin >> t2.hrs >> t2.mins >> t2.secs; computeTimeDifference(t1, t2, &difference); cout << endl << "*Time Difference*: " << t1.hrs << ":" << t1.mins << ":" << t1.secs; cout << " - " << t2.hrs << ":" << t2.mins << ":" << t2.secs; cout << " = " << difference.hrs << ":" << difference.mins << ":" << difference.secs; return 0; } void computeTimeDifference(struct time t1, struct time t2, struct time *difference){ if(t2.secs > t1.secs) { --t1.mins; t1.secs += 60; } difference->secs = t1.secs - t2.secs; if(t2.mins > t1.mins) { --t1.hrs; t1.mins += 60; } difference->mins = t1.mins-t2.mins; difference->hrs = t1.hrs-t2.hrs; } |
Output:
