operator overloading help

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!
Remember way back to how you learned to do subtraction by hand. Remember the concept of "borrowing". In your case, you're going to "borrow" 60 seconds so that you don't have a subtraction that ends in a negative.
0:0:2:3 = 0:0:1:63. I've subtracted one minute, but added 60 seconds, which means I really still have the same value. The resulting operation is straightforward to perform:
    0:0:1:63
-   0:0:1:4


EDIT: Or, perhaps this would be easier. Just go ahead and do the subtraction. When you're done, THEN perform the borrowing. Say you do your calculation and you get 0:0:1:-1. Since seconds is negatve, borrow 60 seconds by subtracting one minute and adding 60 seconds. 0:0:1:-1 becomes 0:0:0:59
Last edited on
Yea that exactly how I did it.

So I did like
if(seconds < 0)
minutes -= 1;
seconds = 60 + t1.seconds - t2.seconds;

and I cannot go over 59 for minutes and seconds and then can't go over 23 for hours.
but sometimes i still get erraneous output. but I did that same code for each, but I still can't get it the way I want it
Show your entire subtraction operator overload code.
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;
    }
}
Don't do the subtraction twice. Say on line 19 you borrow a minute to make up for negative seconds. If you then have to borrow an hour on line 24 and get to line 25, you're adding 60 to the original number of minutes, no longer taking into account the "borrow" you performed on line 19.

So lines like this:
subtract.secs = 60 + (x.secs - y.secs);
should look like this:
subtract.secs += 60;

And don't forget...
return subtract; at the end.
ok I THINK it is working well, except when I plug this in, I get an output of negative 1 for days even though I have an error set that should display 0~00:00:00 if days go below zero

1
2

0~01:01:05 - 0~02:02:10 = -1~22:058:55
Do that check AFTER you do all your subtracting and borrowing. It fits with the flow of moving from the smallest time unit to the largest:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
       
    if(subtract.secs < 0)
    {    
        subtract.mins -= 1;
        subtract.secs += 60;   
    }
    if(subtract.mins < 0)
    {
        subtract.hrs -= 1;
        subtract.mins += 60;
    }
    if(subtract.hrs < 0)
    {               
        subtract.days -= 1;  
        subtract.hrs += 24;
    }
    if(subtract.days < 0)
    {
        subtract.days = 0;                 
        subtract.hrs = 0;     
        subtract.mins = 0;
        subtract.secs = 0;              
    } 
you could just convert the time back to seconds, subtract and then reconvert to dd:hh:mm:ss format

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
time operator-(const time& t2)
{
    this = (this.toSeconds() - t2.toSeconds());
}

int toSeconds() const
{
    return days * 60 * 3600 + hrs * 3600 + mins * 60 + secs;
}
time operator=(int sec)
{
    days = sec / (60*3600);
    sec = sec % (60*3600);
    
    hrs = sec / 3600;
    sec = sec % 3600;

    mins = sec / 60;
    sec = sec % 60;

    secs = sec;

    return *this;
}
Note: Not tested code!
Last edited on
Topic archived. No new replies allowed.