### interval_since function

Hello everyone, I am having trouble getting this function to work properly. Can someone please point me in the right direction.
I was given a class DigitalTime and i need to add a function that will compute the difference of two times. I tried to do it the same way as in the rational number problem but that didn't work.
the times are in 24hr notation
here is the block from main that calls the function

 ``12345`` `````` DigitalTime current(5, 45), previous(2, 30); int hours, minutes; current.interval_since(previous, hours, minutes); cout << "the time interval between " << previous << " and " << current << endl; cout << "is " << hours << " hours and " << minutes << " minutes " << endl; ``````

here is the definition so far I know its bad (I was given the function I just need to figure out the body)

 ``12345678`` ``````void DigitalTime::interval_since(const DigitalTime& a_previous_time, int& hours_in_interval, int& minutes_in_interval)const { DigitalTime result; result.hours = current.hour - previous.hour; result.minutes = current.minute-previous.minuet; return (result.hour, result.minute); }``````

Well, the function doesn't need to return anything, since it is declared as void. The result values are simply stored in the two parameter variables:
`hours_in_interval` and `minutes_in_interval`.

What I would do is to calculate in minutes the difference between the time stored in the current object, and the time passed in the parameter `a_previous_time`, and then convert back to hours and minutes at the end.
First, write a helper function: `DigitalTime::minutes_since_midnight() const ;`
it will come in handy at a lot of places.

 ``1234567891011`` ``````struct DigitalTime { enum { MINUTES_PER_HOUR = 60 } ; int hour ; // invariant: 00 - 23 int minute ; // invariant: 00 - 59 int minutes_since_midnight() const { return hour * MINUTES_PER_HOUR + minute ; } // ... };``````

 ``1234567`` ``````void DigitalTime::interval_since( const DigitalTime& a_previous_time, int& hours_in_interval, int& minutes_in_interval) const { const int total_minutes = minutes_since_midnight() - a_previous_time.minutes_since_midnight() ; // take it up from there }``````

 ``12345678`` ``````void DigitalTime::interval_since(const DigitalTime& a_previous_time, int& hours_in_interval, int& minutes_in_interval)const { { hours_in_interval = hour - a_previous_time.hour; minutes_in_interval = minute - a_previous_time.minute; } }``````

Last edited on
What kind of result does that give with:
 `` `` `` DigitalTime current(5, 45), previous(2, 50);``
i get the expected "3 hours and 15 minuets". But it only works for times of the same day. If I change current to (1, 45) and leave previous alone it returns a negative 1. That doesn't make since you cant have negative time. but for my purpose it will work. I was thinking I might be able to add an if statement to increase the interval by 1 day if current < previous. but not sure
With the test data I suggested, I get this result:
 ```the time interval between 2:50 and 5:45 is 3 hours and -5 minutes```

I'm not sure why you would choose to do this the "hard" way, that is start off with something a bit flaky and then patch it up with ifs and elses.
Last edited on
I'm not ether. I was given everything but the body of the function and was just looking for the easiest way to make it work. Unfortunatly when it comes to code ive found out that what seems like the easy way usually just leads to more problems. like if and else patches

but I wanted to thank you for your help chervil, you have answered almost every question I have posted on this site, in terms I can understand. I would probably be failing if it weren't for you. So thank you for your help.
You're welcome.

It's just that in this case I do think the earlier advice either from JLBorges or myself appears not to have been heeded. Of course it is only a suggestion which you are free to ignore, but my feeling is that doing a single subtraction in terms of minutes (rather than separate subtractions for hours and minutes) will work out simpler in the long run.
Topic archived. No new replies allowed.