# C++: Struct Example: Time

Here is a simple example that defines a new Time struct datatype.

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
    // structs/time-1.cpp - Shows simple use of a struct.
    // Fred Swartz - 2003-09-09

    //================================================ includes
    #include <iostream>
    using namespace std;

    //======================================== define new types
    struct Time {
        int hours;
        int minutes;
        int seconds;
    };

    //============================================== prototypes
    int toSeconds(Time now);

    //==================================================== main
    int main() {
        Time t;
        while (cin >> t.hours >> t.minutes >> t.seconds) {
            cout << "Total seconds: " << toSeconds(t) << endl;
        }
        return 0;
    }

    //=============================================== toSeconds
    int toSeconds(Time now) {
        return 3600*now.hours + 60*now.minutes + now.seconds;
    }
The text from the above example can be selected, copied, and pasted into an editor.