| | |
stuck on a member function for a class
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 7
Reputation:
Solved Threads: 0
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:
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:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cctype> #include <cstdlib> using namespace std; class DigitalTime { public: friend bool operator ==(const DigitalTime& time1, const DigitalTime& time2); DigitalTime(int the_hour, int the_minute); DigitalTime(); void advance(int minutes_added); void advance(int hours_added, int minutes_added); void interval_since(const DigitalTime& a_previous_time, int& hours_in_interval, int& minutes_in_interval); friend istream& operator >>(istream& ins, DigitalTime& the_object); friend ostream& operator <<(ostream& outs, const DigitalTime& the_object); private: int hour; int minute; }; int main() { DigitalTime clock, old_clock, current(5, 45), previous(2, 30); int hours, minutes; string holdprogram; cout << "Enter the time in 24-hour notation: "; cin >> clock; old_clock = clock; clock.advance(15); if (clock == old_clock) cout << "Something is wrong."; cout << "You entered " << old_clock << endl; cout << "15 minutes later the time will be " << clock << endl; clock.advance(2, 15); cout << "2 hours and 15 minutes after that\n" << "the time will be " << clock << endl; current.interval_since(previous, hours, minutes); cout << "The time interval between " << previous << " and " << current << endl << "is " << hours << " hours and " << minutes << " minutes.\n"; cin >> holdprogram; return 0; } void read_hour(istream& ins, int& the_hour); void read_minute(istream& ins, int& the_minute); int digit_to_int(char c); bool operator ==(const DigitalTime& time1, const DigitalTime& time2) { return (time1.hour == time2.hour && time1.minute == time2.minute); } DigitalTime::DigitalTime(int the_hour, int the_minute) { if (the_hour < 0 || the_hour > 23 || the_minute < 0 || the_minute > 59) { cout << "Illegal argument to DigitalTime constructor."; exit(1); } else { hour = the_hour; minute = the_minute; } } DigitalTime::DigitalTime() : hour(0), minute(0) { } void DigitalTime::advance(int minutes_added) { int gross_minutes = minute + minutes_added; minute = gross_minutes%60; int hour_adjustment = gross_minutes/60; hour = (hour + hour_adjustment)%24; } void DigitalTime::advance(int hours_added, int minutes_added) { hour = (hour + hours_added)%24; advance(minutes_added); } void DigitalTime::interval_since(const DigitalTime& a_previous_time, int& hours_in_interval, int& minutes_in_interval) { // i'm stuck here } ostream& operator <<(ostream& outs, const DigitalTime& the_object) { outs << the_object.hour << ':'; if (the_object.minute < 10) outs << '0'; outs << the_object.minute; return outs; } istream& operator >>(istream& ins, DigitalTime& the_object) { read_hour(ins, the_object.hour); read_minute(ins, the_object.minute); return ins; } int digit_to_int(char c) { return ( static_cast<int>(c) - static_cast<int>('0') ); } void read_minute(istream& ins, int& the_minute) { char c1, c2; ins >> c1 >> c2; if (!(isdigit(c1) && isdigit(c2))) { cout << "Error illegal input to read_minute\n"; exit(1); } the_minute = digit_to_int(c1)*10 + digit_to_int(c2); if (the_minute < 0 || the_minute > 59) { cout << "Error illegal input to read_minute\n"; exit(1); } } void read_hour(istream& ins, int& the_hour) { char c1, c2; ins >> c1 >> c2; if ( !( isdigit(c1) && (isdigit(c2) || c2 == ':') ) ) { cout << "Error illegal input to read_hour\n"; exit(1); } if(isdigit(c1) && c2 == ':') { the_hour = digit_to_int(c1); } else { the_hour = digit_to_int(c1)*10 + digit_to_int(c2); ins >> c2; if (c2 != ':') { cout << "Error illegal input to read_hour\n"; exit(1); } } if (the_hour < 0 || the_hour > 23) { cout << "Error illegal input to read_hour\n"; exit(1); } }
Last edited by deicer; Jan 11th, 2008 at 10:55 pm.
>>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.
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.
•
•
Join Date: Dec 2007
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
>>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.
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
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.
•
•
Join Date: Jan 2008
Posts: 7
Reputation:
Solved Threads: 0
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.
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.
Try:
Don't forget to include <math.h>
C++ Syntax (Toggle Plain Text)
void DigitalTime::interval_since(const DigitalTime& a_previous_time, int& hours_in_interval, int& minutes_in_interval) { int hours_dif; int minutes_dif; hours_dif=abs(hour-a_previous_time.hour); minutes_dif=abs(minute-a_previous_time.minute); if(minutes_dif >= 60) { hours_dif+=int(minutes_dif/60); minutes_dif=minutes_dif%60; } hours_in_interval = hours_dif; minutes_in_interval = minutes_dif; }
Don't forget to include <math.h>
An Apple a Day keeps a Doctor away!
•
•
Join Date: Jan 2008
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
Try:
C++ Syntax (Toggle Plain Text)
void DigitalTime::interval_since(const DigitalTime& a_previous_time, int& hours_in_interval, int& minutes_in_interval) { int hours_dif; int minutes_dif; hours_dif=abs(hour-a_previous_time.hour); minutes_dif=abs(minute-a_previous_time.minute); if(minutes_dif >= 60) { hours_dif+=int(minutes_dif/60); minutes_dif=minutes_dif%60; } hours_in_interval = hours_dif; minutes_in_interval = minutes_dif; }
Don't forget to include <math.h>
![]() |
Similar Threads
- Why use dynamic cast (C++)
- C++ TMoney Class - I Can't Figure This Out. (C++)
- help with classes?! (C++)
- File Handling using C++ (C++)
- Classes-- stuck in the mire of syntax (C++)
- Inheritance & Derived Classes (C++)
- A little Help (C++)
Other Threads in the C++ Forum
- Previous Thread: I need help with getting a header and 2 files set up.
- Next Thread: i dont know how to continue...
Views: 1267 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






