Solved

HW Help on classes

Posted on 2002-03-08
8
299 Views
Last Modified: 2010-04-01
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!
0
Comment
Question by:jwnrb
  • 5
  • 3
8 Comments
 
LVL 4

Expert Comment

by:ct.smith
Comment Utility
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
 

Author Comment

by:jwnrb
Comment Utility
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
 
LVL 4

Expert Comment

by:ct.smith
Comment Utility
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
 

Author Comment

by:jwnrb
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Accepted Solution

by:
ct.smith earned 50 total points
Comment Utility
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
 

Author Comment

by:jwnrb
Comment Utility
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
 

Author Comment

by:jwnrb
Comment Utility
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
 

Author Comment

by:jwnrb
Comment Utility
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 115
std::atomic with custom class 6 153
Socket Programming (Unix) 8 98
Arduino EDI - Programming language 3 60
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now