5 Replies - 372 Views - Last Post: 16 February 2010 - 10:32 AM Rate Topic: -----

#1 react05  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: Help with - operator

Posted 15 February 2010 - 07:06 PM

View Postreact05, 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.
Was This Post Helpful? 0
  • +
  • -

#3 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Help with - operator

Posted 15 February 2010 - 07:45 PM

Well, I remember reading something about this on MSDN. I think M$ has done it for you.

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

Was This Post Helpful? 0
  • +
  • -

#4 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 react05  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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);
}

Was This Post Helpful? 0
  • +
  • -

#6 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1