stuck on a member function for a class

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 7
Reputation: deicer is an unknown quantity at this point 
Solved Threads: 0
deicer deicer is offline Offline
Newbie Poster

stuck on a member function for a class

 
0
  #1
Jan 11th, 2008
I have the following code which is working properly, what I'm looking to do is add an additional function to it, but I'm getting stuck.

What I need to do is add a function to my class called DigitalTime::interval_since(const DigitalTime& a_previous_time, int& hours_in_interval, int& minutes_in_interval)

What this function does is computes the time interval between two values of type DigitalTime. One of the values of type DigitalTime is the object that calls the member function interval_since, and the other value of type DigitalTime is given as the first argument.

So basically I'm looking to call it like so:
DigitalTime current(5, 45), previous(2, 30);
int hours, minutes;

current.interval_since(previous, hours, minutes);
cout << "The time interval between " << previous
<< " and " << current << endl
<< "is " << hours << " hours and "
<< minutes << " minutes.\n";

I'm really having trouble figuring out how to create this function, any help would be most appreciated.

Here is the full code if it helps:
  1. #include <iostream>
  2. #include <cctype>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. class DigitalTime
  8. {
  9. public:
  10. friend bool operator ==(const DigitalTime& time1, const DigitalTime& time2);
  11.  
  12. DigitalTime(int the_hour, int the_minute);
  13.  
  14. DigitalTime();
  15.  
  16. void advance(int minutes_added);
  17.  
  18. void advance(int hours_added, int minutes_added);
  19.  
  20. void interval_since(const DigitalTime& a_previous_time, int& hours_in_interval, int& minutes_in_interval);
  21.  
  22. friend istream& operator >>(istream& ins, DigitalTime& the_object);
  23.  
  24. friend ostream& operator <<(ostream& outs, const DigitalTime& the_object);
  25. private:
  26. int hour;
  27. int minute;
  28. };
  29.  
  30. int main()
  31. {
  32. DigitalTime clock, old_clock, current(5, 45), previous(2, 30);
  33. int hours, minutes;
  34. string holdprogram;
  35.  
  36. cout << "Enter the time in 24-hour notation: ";
  37. cin >> clock;
  38.  
  39. old_clock = clock;
  40. clock.advance(15);
  41.  
  42. if (clock == old_clock)
  43. cout << "Something is wrong.";
  44. cout << "You entered " << old_clock << endl;
  45. cout << "15 minutes later the time will be "
  46. << clock << endl;
  47.  
  48. clock.advance(2, 15);
  49. cout << "2 hours and 15 minutes after that\n"
  50. << "the time will be "
  51. << clock << endl;
  52.  
  53. current.interval_since(previous, hours, minutes);
  54. cout << "The time interval between " << previous
  55. << " and " << current << endl
  56. << "is " << hours << " hours and "
  57. << minutes << " minutes.\n";
  58.  
  59. cin >> holdprogram;
  60.  
  61. return 0;
  62. }
  63.  
  64. void read_hour(istream& ins, int& the_hour);
  65.  
  66. void read_minute(istream& ins, int& the_minute);
  67.  
  68. int digit_to_int(char c);
  69.  
  70. bool operator ==(const DigitalTime& time1, const DigitalTime& time2)
  71. {
  72. return (time1.hour == time2.hour && time1.minute == time2.minute);
  73. }
  74.  
  75. DigitalTime::DigitalTime(int the_hour, int the_minute)
  76. {
  77. if (the_hour < 0 || the_hour > 23 || the_minute < 0 || the_minute > 59)
  78. {
  79. cout << "Illegal argument to DigitalTime constructor.";
  80. exit(1);
  81. }
  82. else
  83. {
  84. hour = the_hour;
  85. minute = the_minute;
  86. }
  87. }
  88.  
  89. DigitalTime::DigitalTime() : hour(0), minute(0)
  90. {
  91.  
  92. }
  93.  
  94. void DigitalTime::advance(int minutes_added)
  95. {
  96. int gross_minutes = minute + minutes_added;
  97. minute = gross_minutes%60;
  98.  
  99. int hour_adjustment = gross_minutes/60;
  100. hour = (hour + hour_adjustment)%24;
  101. }
  102.  
  103. void DigitalTime::advance(int hours_added, int minutes_added)
  104. {
  105. hour = (hour + hours_added)%24;
  106. advance(minutes_added);
  107. }
  108.  
  109. void DigitalTime::interval_since(const DigitalTime& a_previous_time, int& hours_in_interval, int& minutes_in_interval)
  110. {
  111. // i'm stuck here
  112. }
  113.  
  114. ostream& operator <<(ostream& outs, const DigitalTime& the_object)
  115. {
  116. outs << the_object.hour << ':';
  117. if (the_object.minute < 10)
  118. outs << '0';
  119. outs << the_object.minute;
  120. return outs;
  121. }
  122.  
  123. istream& operator >>(istream& ins, DigitalTime& the_object)
  124. {
  125. read_hour(ins, the_object.hour);
  126. read_minute(ins, the_object.minute);
  127. return ins;
  128. }
  129.  
  130. int digit_to_int(char c)
  131. {
  132. return ( static_cast<int>(c) - static_cast<int>('0') );
  133. }
  134.  
  135. void read_minute(istream& ins, int& the_minute)
  136. {
  137. char c1, c2;
  138. ins >> c1 >> c2;
  139.  
  140. if (!(isdigit(c1) && isdigit(c2)))
  141. {
  142. cout << "Error illegal input to read_minute\n";
  143. exit(1);
  144. }
  145.  
  146. the_minute = digit_to_int(c1)*10 + digit_to_int(c2);
  147.  
  148. if (the_minute < 0 || the_minute > 59)
  149. {
  150. cout << "Error illegal input to read_minute\n";
  151. exit(1);
  152. }
  153. }
  154.  
  155. void read_hour(istream& ins, int& the_hour)
  156. {
  157. char c1, c2;
  158. ins >> c1 >> c2;
  159.  
  160. if ( !( isdigit(c1) && (isdigit(c2) || c2 == ':') ) )
  161. {
  162. cout << "Error illegal input to read_hour\n";
  163. exit(1);
  164. }
  165.  
  166. if(isdigit(c1) && c2 == ':')
  167. {
  168. the_hour = digit_to_int(c1);
  169. }
  170. else
  171. {
  172. the_hour = digit_to_int(c1)*10 + digit_to_int(c2);
  173. ins >> c2;
  174. if (c2 != ':')
  175. {
  176. cout << "Error illegal input to read_hour\n";
  177. exit(1);
  178. }
  179. }
  180.  
  181. if (the_hour < 0 || the_hour > 23)
  182. {
  183. cout << "Error illegal input to read_hour\n";
  184. exit(1);
  185. }
  186. }
Last edited by deicer; Jan 11th, 2008 at 10:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,648
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1498
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: stuck on a member function for a class

 
0
  #2
Jan 11th, 2008
>>I'm really having trouble figuring out how to create this function
How to creat the function or how to calculate the time interval? To compute the interval you also need the date (day, month and year) because time intervals can span days or even years. Believe it or now but there are standard C funcions to do that -- see difftime function in time.h.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 11
Reputation: tehprince is an unknown quantity at this point 
Solved Threads: 0
tehprince tehprince is offline Offline
Newbie Poster

Re: stuck on a member function for a class

 
0
  #3
Jan 12th, 2008
Originally Posted by Ancient Dragon View Post
>>I'm really having trouble figuring out how to create this function
How to creat the function or how to calculate the time interval? To compute the interval you also need the date (day, month and year) because time intervals can span days or even years. Believe it or now but there are standard C funcions to do that -- see difftime function in time.h.
It's based on 24 hour time and within the same day. So I don't need to worry about the date.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,648
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1498
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: stuck on a member function for a class

 
0
  #4
Jan 12th, 2008
convert both times to seconds then subtract the two. Finally convert the difference to normal hours, minutes and seconds. To convert to seconds = hours * 3600 + minutes * 60 + seconds. Converting the difference back you need to use the mod operator % and division operator /. Example: hours = diff/3600, and the remaing seconds is diff % 3600. That will become the new diff to calculate the minutes.

To illustrate: assume the difference is below. Here we use integer arithmetic, so there are no fractions.
diff = 1 hour 24 minutes 30 seconds = 5070 seconds

Now to convert the seconds (5070) back to hours, minutes and seconds, do this:
hours = 5070/3600 = 1
new diff = 5070 % 3600 = 1470

minutes = 1470/60 = 24
new diff = 1470 % 60 = 30

seconds = 30
Last edited by Ancient Dragon; Jan 12th, 2008 at 3:20 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 7
Reputation: deicer is an unknown quantity at this point 
Solved Threads: 0
deicer deicer is offline Offline
Newbie Poster

Re: stuck on a member function for a class

 
0
  #5
Jan 12th, 2008
Here's what I don't get about the function I need to add:

DigitalTime::interval_since(const DigitalTime& a_previous_time, int& hours_in_interval, int& minutes_in_interval)

What the heck am I passing in through the "hours_in_interval" and "minutes_in_interval"? Why is this even (keep in mind I need to implement this for a school project, so I have to create the function as specified above).

DigitalTime current(5, 45), previous(2, 30);
int hours, minutes;

current.interval_since(previous, hours, minutes);
cout << "The time interval between " << previous
<< " and " << current << endl
<< "is " << hours << " hours and "
<< minutes << " minutes.\n";

Ok, so the function is getting passed two times here correct? The calling objects "current" and the 1st argument "previous", which are both DigitialTime objects. Now where the heck or what the heck is the purpose of the last two arguments, hours and minutes?

It seems simpler to me to call the function like so: interval_since(current, previous)

But as I said this is for school and it has to be created as specified above. Still a bit stuck.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 114
Reputation: zhelih has a little shameless behaviour in the past 
Solved Threads: 11
zhelih's Avatar
zhelih zhelih is offline Offline
Junior Poster

Re: stuck on a member function for a class

 
0
  #6
Jan 12th, 2008
Is your code that you've posted correct? I've trouble with running it.
An Apple a Day keeps a Doctor away!
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 11
Reputation: tehprince is an unknown quantity at this point 
Solved Threads: 0
tehprince tehprince is offline Offline
Newbie Poster

Re: stuck on a member function for a class

 
0
  #7
Jan 12th, 2008
Originally Posted by zhelih View Post
Is your code that you've posted correct? I've trouble with running it.
Odd, it works for me, it has a couple warnings while compiling, but that's from the members variables of the function interval_since not being used.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 114
Reputation: zhelih has a little shameless behaviour in the past 
Solved Threads: 11
zhelih's Avatar
zhelih zhelih is offline Offline
Junior Poster

Re: stuck on a member function for a class

 
0
  #8
Jan 12th, 2008
Try:

  1. void DigitalTime::interval_since(const DigitalTime& a_previous_time, int& hours_in_interval, int& minutes_in_interval)
  2. {
  3. int hours_dif;
  4. int minutes_dif;
  5. hours_dif=abs(hour-a_previous_time.hour);
  6. minutes_dif=abs(minute-a_previous_time.minute);
  7. if(minutes_dif >= 60) {
  8. hours_dif+=int(minutes_dif/60);
  9. minutes_dif=minutes_dif%60;
  10. }
  11. hours_in_interval = hours_dif;
  12. minutes_in_interval = minutes_dif;
  13.  
  14. }

Don't forget to include <math.h>
An Apple a Day keeps a Doctor away!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 7
Reputation: deicer is an unknown quantity at this point 
Solved Threads: 0
deicer deicer is offline Offline
Newbie Poster

Re: stuck on a member function for a class

 
0
  #9
Jan 12th, 2008
Originally Posted by zhelih View Post
Try:

  1. void DigitalTime::interval_since(const DigitalTime& a_previous_time, int& hours_in_interval, int& minutes_in_interval)
  2. {
  3. int hours_dif;
  4. int minutes_dif;
  5. hours_dif=abs(hour-a_previous_time.hour);
  6. minutes_dif=abs(minute-a_previous_time.minute);
  7. if(minutes_dif >= 60) {
  8. hours_dif+=int(minutes_dif/60);
  9. minutes_dif=minutes_dif%60;
  10. }
  11. hours_in_interval = hours_dif;
  12. minutes_in_interval = minutes_dif;
  13.  
  14. }

Don't forget to include <math.h>
You sir, are a genius!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1267 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC