HW Help on classes

Hi,

I am doing some hw on classes, and i am really stuck on a member function i am working on.

i need to subtract two time stamps, and return the difference.

void TimeStamp::diff( TimeStamp &ts2, int &weeks, int &days, int &hours, int &minutes ) {
   // subtract 2 timestamps
   // convert to weeks, days, hours, minutes...
   // weeks =  days / 7;
   // days  =  day % 7;
   // days += 365 * years;
   // year    day   hour   minute
   // ----    ---   ----   ------
   //   y1     d1     h1       m1
   // - y2     d2     h2       m2
   // ---------------------------
   // [  ]    [ ]   [  ]   [    ]
   // look out for negative numbers, where as if m1 and m2 return neg, u need to compestate by adding then to the hour column




}


this is the def:

class TimeStamp {
   private:
      int year;
      int month;
      int day;
      int hour;
      int minute;
      static const int daysInMonth[ 13 ];
   public:
      TimeStamp() {};
      TimeStamp( int yr, int mo, int da, int ho, int mi );
      void display();
      bool operator >( TimeStamp &rhs );
      void diff( TimeStamp &ts2, int &weeks, int &days, int &hours, int &minutes );
   private:
      int dayofyear();
};


what i cant figure out is, how do i get the data?  the user puts in from standard input teh values of t1 and t2, and i need to find the difference, but the reading of those values (at least as of now) arent stored in private data....so i am really puzzled in what to do....

actually, very stuck, i just need a bit of help to start me off so i can go on my way....

i hope i gave enough info for some1 to help me a bit.

thanks!
jwnrbAsked:
Who is Participating?
 
ct.smithConnect With a Mentor Commented:
I think you may want to change the part with your second input.

Something like:

cin >> yy2 >> mm2 >> dd2 >> hh2 >> mi2;
TimeStamp ts2(yy2, mm2, dd2, hh2, mi2);

// variables to hold the output
int year, month, day, hour, minute;
ts1.diff(ts2, year, month, day, hour, minute);

TimeStamp result(year, month, day, hour, minute);
result.display();


Your diff function should use the private members of ts1 and ts2 to calculate the appropriate values for your difference result.
0
 
ct.smithCommented:
If I understand your question right, you're just trying to get the input into your TimeStamp Object.  Just read into temp variables and pass them in to your object.

TimeStamp t;
int yr, int mo;
cin >> yr >> mo;
t.setYear(yr);
t.setMonth(mo);
0
 
jwnrbAuthor Commented:
in the driver program, pretty much i got:  ts1 = TimeStamp(yy1,mm1,dd1,hh1,mi1);

the only hint the professor gave me was this: time1 is the object that invoked the member function, and contains year, month, day, hour, and minute.  time2 is ts2, and contains ts2.year, ts2.month, etc.

so pretty much it would be called t1.diff(t2,foo,foo,foo,foo)??

i am so beyong stuck, this was due almost two days ago, so i am losing points so quickly just bc i cant get this function to work, so i can finally implement everything else1!!!

so i know this is for my own beneift and i dont want to cheat bc i need to learn this, but at least some1 help me start something out...this function is such a small part but dont write the function either!!  i need to learn!  but i just dont get this at all!  i can do it on paper, but i cant see how the two timestamps gets to the function to begin with!!!
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
ct.smithCommented:
Note that the diff member function has its arguments passed by reference.  This is usually only done so the function can set the value of the variable as a result.  So, when I call diff, I would expect that those parameters contain meaningful  values when the function is done (ie. the result of your difference calculation).

So, instead of t1.diff(t2, foo, foo, foo, foo);
do:
int year, month, day, hour, minute;
t1.diff(t2, year, month, day, hour, minute);

Now, with that bit of insight, it should be straightforward to implement diff such that it sets the year, month, day, hout and minute appropriately.

0
 
jwnrbAuthor Commented:
t1.diff(t2,year..) should use the private data members?

bc

i am doing something like this


   cout << "\nEnter current date and time (yyyy, mm, dd, hh, mm): ";
   cin >> yy1 >> mm1 >> dd1 >> hh1 >> mi1;

   if ( mm1 > 12 || mm1 < 1 ) {
      cerr << "Invalid month: " << mm1 << "!\n";
      exit(1);
   }

 //  if ( ts1.daysInMonth[dd1] != dd1 ) {
 //    cerr << "Month " << dd1 << " does not have " << ts1.daysInMonth[dd1] << " days!\n";
 //     exit(1);
 //  }

   if ( hh1 < 0 || hh1 > 12 ) {
      cerr << "Hour " << hh1 << " is invalid!\n";
      exit(1);
   }

   if ( mi1 > 59 ) {
      cerr << "Minute " << mi1 << " is invalid!\n";
      exit(1);
   }


   ts1 = TimeStamp(yy1,mm1,dd1,hh1,mi1);
   cout << "\nEnter activity (I = In, O = Out): ";
   cin >> choice;

   if ( choice == 'I' || choice == 'i' ) {
      cout << "Enter date and time when car was rented: ";
      cin >> yy2 >> mm2 >> dd2 >> hh2 >> mi2;
      ts1.diff(ts2,mm2,dd2,hh2,mi2);
      ts1.display();
   }

   else
      cerr << "Option not specified\n";




i used temp vars for both timestamps to fill them up.

pretty much this prog is supposed to simulate a rental system...i enter in the date of today, then enter the date it was checked out.  thus, find teh difference in time, output the charges accordingly.

thats why i need the end result in weeks, days, hours, also why i need to worry about negative results, bc if i checked out at 40 minutes and returned at 50 minutes, it will get negative number.  then my next worry is to have an overloaded operator > so i can check if t1 > t2 to see if they hadnt messed up!!  hmm, so if i use the private data members for that, then i should be able to access that data then there...hmmm, lemme play around and maye you can comnent on some of this for further clarication...thnx
0
 
jwnrbAuthor Commented:
well before i got this email, this is what i got as results:  (so far)

~/275 $ g++ tdriver.cc timestamp.cc
~/275 $ a.out

Enter current date and time (yyyy, mm, dd, hh, mm): 2000 12 12 12 12

Enter activity (I = In, O = Out): 2001 12 12 12 12
Option not specified
~/275 $ a.out

Enter current date and time (yyyy, mm, dd, hh, mm): 2000 12 12 12 12

Enter activity (I = In, O = Out): i
Enter date and time when car was rented: 2001 12 12 12 12
min2: 134552480
~/275 $


=======diff=====

int minute2 = ts2.minute;
   int hour2   = ts2.hour;
   int day2    = ts2.day;
   int month2  = ts2.month;
   int year2   = ts2.year;

   cout << "min2: " << minute2 << endl;

=====instance of the diff------
ts1.diff(ts2,weeks,days,hours,minutes);
0
 
jwnrbAuthor Commented:
well before i got this email, this is what i got as results:  (so far)

~/275 $ g++ tdriver.cc timestamp.cc
~/275 $ a.out

Enter current date and time (yyyy, mm, dd, hh, mm): 2000 12 12 12 12

Enter activity (I = In, O = Out): 2001 12 12 12 12
Option not specified
~/275 $ a.out

Enter current date and time (yyyy, mm, dd, hh, mm): 2000 12 12 12 12

Enter activity (I = In, O = Out): i
Enter date and time when car was rented: 2001 12 12 12 12
min2: 134552480
~/275 $


=======diff=====

int minute2 = ts2.minute;
   int hour2   = ts2.hour;
   int day2    = ts2.day;
   int month2  = ts2.month;
   int year2   = ts2.year;

   cout << "min2: " << minute2 << endl;

=====instance of the diff------
ts1.diff(ts2,weeks,days,hours,minutes);
0
 
jwnrbAuthor Commented:
well you helped me start the class which is what i wanted...developing the algorithm is my problem now, but i do need to learn, at least the class compiles and everything else is coming in smoothly.

thanks!
0
All Courses

From novice to tech pro — start learning today.