conversion of time in seconds using structure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • haniya ali
    New Member
    • Sep 2013
    • 6

    conversion of time in seconds using structure

    i create a structure called time. Its three members, all type int called hours, minutes, and seconds. This is in 12:59:59 format and i finally want to print out the total number of seconds represented by this time value.
    long totalsecs = t1.hours*3600 + t1.minutes*60 + t1.seconds
    i am using this formula but facing errors can any one solve it??
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Hi haniya ali and welcome to bytes.com!

    At first glance the formular looks ok, so you'll have to be a little more specific: What errors are you facing? What goes wrong and what are you expecting?

    Comment

    • haniya ali
      New Member
      • Sep 2013
      • 6

      #3
      Code:
      struct TIME
          { 
          int seconds; 
          int minutes;
          int hours; 
           };
          void totalsecs(struct TIME t1,struct TIME*totalsecs ); 
      using namespace std;
      
      int main()
      {     
          struct TIME t1,totalsecs,hours,minutes,; 
          cout<<"Enter the time: \n";
          cout<<"Enter hours, minutes and seconds respectively: "; 
          cin>>t1.hours>>t1.minutes>>t1.seconds;
          cout<<t1.hours<<":"<<t1.minutes<<":"<<t1.seconds;
      here is the whole code it works properly but when i apply the formula to calclute seconds then give error
      Last edited by Rabbit; Sep 26 '13, 03:42 AM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        You're declaring totalsecs as a struct TIME in line 12 and in your formular as a long. Such double declarations are not allowed in C/C++; here's a version that works:
        Code:
        #include<iostream>
        
        struct TIME
            { 
            int seconds; 
            int minutes;
            int hours; 
             };
            void totalsecs(struct TIME t1,struct TIME*totalsecs ); 
        using namespace std;
         
        int main()
        {     
            struct TIME t1; // totalsecs and others probably aren't of type "TIME", are they?
            cout<<"Enter the time: \n";
            cout<<"Enter hours, minutes and seconds respectively: "; 
            cin>>t1.hours>>t1.minutes>>t1.seconds;
            cout<<t1.hours<<":"<<t1.minutes<<":"<<t1.seconds<<endl;
            long totalsecs = t1.hours*3600 + t1.minutes*60 + t1.seconds;
            cout<<"That makes "<<totalsecs<<" seconds"<<endl;
        	
        	return 0;
        }

        Comment

        • haniya ali
          New Member
          • Sep 2013
          • 6

          #5
          thank you i really works now.

          Comment

          Working...