# Help with - operator

Page 1 of 1

## 5 Replies - 604 Views - Last Post: 16 February 2010 - 10:32 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=156248&amp;s=a8ceab223b1eb0d020cd3bff08cd4f9b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 react05

Reputation: 0
• Posts: 61
• Joined: 29-September 09

# Help with - operator

Posted 15 February 2010 - 07:00 PM

Hey guys, from the code i have below. how would i find the difference between two times using the -operator? Not in hours but in minutes.
Thanks!
```#include <iostream>
#include <string>
#include <cstring>
using namespace std;
#include "Time.h"

Time::Time( int hour, int minute )
{
hour = 0;
minute = 0;
}

istream & operator>> (istream & in, Time & t)
{

string time;
string hour;
string minute;
int timehour;
int timeminute;
in >> time;
hour = time.substr(0,2);
minute = time.substr(3,2);
timehour = atoi(hour.c_str());
timeminute = atoi(minute.c_str());
t.hour = timehour;
t.minute = timeminute;
return in;

}

ostream & operator<< (ostream & out, Time & t)
{
out << t.hour << " : " << t.minute << endl;
return out;

}
```

This post has been edited by react05: 15 February 2010 - 07:01 PM

Is This A Good Question/Topic? 0

## Replies To: Help with - operator

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: Help with - operator

Posted 15 February 2010 - 07:06 PM

react05, on 15 February 2010 - 08:00 PM, said:

Hey guys, from the code i have below. how would i find the difference between two times using the -operator? Not in hours but in minutes.
Thanks!
```#include <iostream>
#include <string>
#include <cstring>
using namespace std;
#include "Time.h"

Time::Time( int hour, int minute )
{
hour = 0;
minute = 0;
}

istream & operator>> (istream & in, Time & t)
{

string time;
string hour;
string minute;
int timehour;
int timeminute;
in >> time;
hour = time.substr(0,2);
minute = time.substr(3,2);
timehour = atoi(hour.c_str());
timeminute = atoi(minute.c_str());
t.hour = timehour;
t.minute = timeminute;
return in;

}

ostream & operator<< (ostream & out, Time & t)
{
out << t.hour << " : " << t.minute << endl;
return out;

}
```

Not responding per se, but quoting the OP as he/she keeps deleting his/her content after getting answers, which is rude behavior for a forum.

### #3 taylorc8

• B&

Reputation: 150
• Posts: 1,572
• Joined: 21-July 09

## Re: Help with - operator

Posted 15 February 2010 - 07:45 PM

This post has been edited by taylorc8: 15 February 2010 - 07:46 PM

### #4 anonymouscodder

Reputation: 126
• Posts: 710
• Joined: 01-January 10

## Re: Help with - operator

Posted 15 February 2010 - 08:15 PM

You need help with the calculation or with the operator overload?

### #5 react05

Reputation: 0
• Posts: 61
• Joined: 29-September 09

## Re: Help with - operator

Posted 15 February 2010 - 08:26 PM

with the calculation.
i think i got it though. i got this
```int Time::operator-(const Time &t)
{
int m1, m2;
m1 = hour*60 + minute;
m2 = (t.hour)*60 + t.minute;
return (m1 - m2);
}
```

### #6 anonymouscodder

Reputation: 126
• Posts: 710
• Joined: 01-January 10

## Re: Help with - operator

Posted 16 February 2010 - 10:32 AM

I think it's a good idea to return a Time instance instead of an int. I would do:
1. Do the calculation.
2. Converts the calculation result to the constructor (or set method) format (hours? minutes? seconds?).
3. Creates a Time object with the calculated value.
4. Returns the new Time instance.

Returning a Time instance is better because you don't have to worry if the operation returns a value in seconds, minutes, hours, or whatever, it's just Time. But by doing this way you have to allow that Time instances can have negative values.