# Conversion functions

• 09-03-2003
Bones
Conversion functions
I have to Add to the Time class the conversion function for converting from a long (seconds 77777) to a Time object....I have no idea what to do...this is the code I have so far....

Code:

``` class Time   {   private:       int hrs;                      //1 to 23       int mins;                      //0 to 59       int secs;                            //0 to 59   public:                          //no-arg constructor       Time() : hrs(0), mins(0), secs(0)         {  }                                     //3-arg constructor       Time(int h, int m, int s) : hrs(h), mins(m), secs(s)         {  }       void display() const          //format: 11:59 p.m.         {         cout << hrs << ':' << mins << ':' << secs;         }         Time operator ++ ()        // increment prefix         { return Time (++hrs, ++ mins, ++ secs);}         Time operator ++ (int)      // increment postfix         { return Time (hrs++, mins++, secs++);}         Time operator -- ()        //prefix         {return Time (--hrs, --mins, --secs);}         Time operator -- (int)      //postfix         {return Time (hrs--, mins--, secs--);} }; //////////////////////////////////////////////////////////////// void main(void)   {   int h, m, s;   long time;   cout << "Enter 24-hour time: \n";   cout << "  Hours (0 to 23): "; cin >> h;   cout << "  Minutes: ";  cin >> m;   cout << "  Seconds: ";  cin >> s;   Time t(h, m, s);          //make a time object t   cout << "You entered: ";  //display the t   t.display();   } I think i need a conversion operator ... but not to sure how to go about it....plz help.....thx```
• 09-03-2003
joshdick
Re: Conversion functions
Quote:

Originally posted by Bones
Code:

`      int hrs;                      //1 to 23`

Hours go from zero to 23, as you were using them in the rest of your class.

Quote:

Code:

```      void display() const          //format: 11:59 p.m.         {         cout << hrs << ':' << mins << ':' << secs;         }```

Code:

```t.display(); //you could do this: cout << t;```
Okay, and about those increment and decrement operators, your implementation is wrong. Time does not progress by an hour, minute, and second. It progresses by one second at a time. The implementation of your increment operator needs to reflect that basic understanding of time. I'm not sure why you need a decrement operator, unless of course, you've discovered time travel in which case, your talents are far greater in physics than programming :p

Quote:

Code:

`void main(void)`

No. Noooooooooo. Bad coder. Here's what you should do:
Code:

```int main() {     // statements and such     return 0; }```
Quote:

Code:

```  cout << "Enter 24-hour time: \n";   cout << "  Hours (0 to 23): "; cin >> h;   cout << "  Minutes: ";  cin >> m;   cout << "  Seconds: ";  cin >> s;```

Please put seperate expressions on seperate lines. That code there is difficult to read.

And, instead of having them enter the hours, minutes, and seconds seperately, you could overload the >> operator so that the user could enter time all at once in the form HH:MM:SS.

I hope that helps :)
• 09-03-2003
joshdick
Whoops, I didn't exactly answer your question. Silly me.

You can write a constructor that takes a single argument of seconds and then convert that into minutes and seconds and then hours, minutes, and seconds.

Code:

```Time(int seconds) {     secs = seconds % 60;     mins = seconds / 60;     hrs = mins / 60;     mins = mins % 60; } // Prove to myself that this works: time(7000); secs = 7000 % 60; secs = 40; mins = 7000 / 60; mins = 101; hrs = 1; mins = 101 / 60; mins = 41; time(7000) == 1:41:40;```
Okay, that looks like a solution to me.
• 09-03-2003
Dave_Sinkula
Quote:

Originally posted by joshdick
// Prove to myself that this works:
time(7000) == 1:41:40;
Okay, that looks like a solution to me.
You might want to find the 15 minutes that are missing.

7000 seconds != 1:41:40
7000 seconds == 1:56:40
[/edit]
• 09-03-2003
swoopy
Quote:

Originally posted by Dave_Sinkula
You might want to find the 15 minutes that are missing.
He just discovered time travel?
• 09-03-2003
joshdick
Quote:

Originally posted by swoopy
He just discovered time travel?
Hmmm, I always wanted to do that.