Convert Time

This is a discussion on Convert Time within the C++ Programming forums, part of the General Programming Boards category; For one of my assignments we are given the following code... Code: #include <iostream> #include <string> #include <conio.h> using namespace ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    22

    Convert Time

    For one of my assignments we are given the following code...

    Code:
    #include <iostream>
    #include <string>
    #include <conio.h>
    using namespace std;
    ////////////////////////////////////////////////////////////////
    
    
    class Time
    {
       private:
          const float secTime;            // seconds to time
          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), secTime(60)
             {  }
                                         //3-arg constructor
          Time(int h, int m, int s) : hrs(h), mins(m), secs(s), secTime(60)
             {  }
          void display() const           //format: 11:59 p.m.
             {
    
                    if (hrs <  10) cout << '0';
                    cout << hrs << ':';
                    if (mins < 10) cout <<'0';
                    cout << mins <<':' ;
                    if (secs <10) cout << '0';
                    cout <<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--);}
    
             Time ( float seconds ) : secTime(60)   //convert from long to Time
             {
                    float fltmin = seconds / secTime;
                    float flthrs = fltmin / secTime;
                    hrs = int(flthrs);
    
                    float min = secTime * (flthrs - hrs) ;
                    mins = int(min);
    
                    float fltsec = (min - mins) * 100;
    
                    secs = int(fltsec);
    
    
             }
    
              operator float()
            {
                int convert;
                float num;
    
                convert = (hrs * secTime * secTime) + (mins * secTime) + (secs);
    
                num = static_cast<float>(convert);
    
                return num;
            }
    
    
    
    
    
    };
    
    
    
    int main()
       {
       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();
    
    
       Time t2=t++;
    
       cout << "\n t2 is " ; t2.display(); cout << ". t is "; t.display();
       cout <<"\tPostfix addition.";
       Time t3=++t;
       cout << "\n t3 is " ; t3.display(); cout << ". t is "; t.display();
       cout <<"\tPrefix addition";
       Time t4=t--;
       cout << "\n t4 is " ; t4.display(); cout << ". t is "; t.display();
       cout << "\tPostfix subtraction.";
       Time t5=--t;
       cout << "\n t5 is " ; t5.display(); cout << ". t is "; t.display();
       cout << "\tPrefix subtraction.";
    
    
            getch ();
    
            return 0;
    
    }
    Some how when the time goes past 23:59:59 we have to change it to 00:00:00. Then when we use the -- operator the time has to go back to 23:59:59... I understand this is not how time works....but it is an exercise to help us with overloading operators.....if anyone can help that would be great...
    Thanks

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Originally posted by joshdick
    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.
    That quote was from an earlier thread. I think you should heed its advice. In order to do that, you must increment and decrement the secs variable only. Before you do that you will have to check to see if the change will make secs go above 59 or below 00 -- if so you will also have to increment or decrement the mins value in the same way (checking it for a valid value). If that will produce a a number above 59 or below 00, then increment the hrs variable. Finally, if hrs will go above 23 or below 00, change it directly to the appropriate number.

    That is certainly more logic than you currently have in your operators. Because of that, I would suggest you implement one in terms of the other. For example:
    Code:
    class ExInt
    {
        int internalInt;
    public:
        ExInt() : internalInt(0) { }
        ~ExInt() { }
    
        ExInt& operator++()
        {
            ++internalInt;  // Actual logic goes here
            return *this;
        }
        ExInt operator++(int)
        {
            ExInt old = *this;
            operator++();
            return *old;
        }
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 03:03 AM
  2. Time in milliseconds
    By coder_009 in forum C Programming
    Replies: 4
    Last Post: 05-09-2008, 04:36 AM
  3. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 08:56 AM
  4. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 05:57 PM
  5. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21