Hi I am creating a class called time and we've had to do operator overloading for <, > , <=, >=, ==, !=, ++, --, >>, <<, * , +, and -.
Well I have done and error checked them all. The only one I cannot seem to get right is the minus and its because of the error checking. I am having issues with times like this
t1 = 0:0:2:3
t2 = 0:0:1:4
t1 - t2 should equal 0:0:0:59 but it returns 0:0:1:-1.
(days:hours:minutes:seconds)
I need it to check for all cases and I just do not know how.
PLEASE HELP!
Here is the code I have so far:
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
|
time operator- (const time& x, const time& y)
{
time subtract;
subtract.days = x.days - y.days;
subtract.hrs = x.hrs - y.hrs;
subtract.mins = x.mins - y.mins;
subtract.secs = x.secs - y.secs;
if(subtract.days < 0)
{
subtract.days = 0;
subtract.hrs = 0;
subtract.mins = 0;
subtract.secs = 0;
}
if(subtract.secs < 0)
{
subtract.mins -= 1;
subtract.secs = 60 + (x.secs - y.secs);
}
if(subtract.mins < 0)
{
subtract.hrs -= 1;
subtract.mins = 60 + (x.mins - y.mins);
}
if(subtract.hrs < 0)
{
subtract.days -= 1;
subtract.hrs = 24 + (x.hrs - y.hrs);
}
else
{
subtract.days;
subtract.hrs;
subtract.mins;
subtract.secs;
}
}
|