Seconds Converter:

This is a discussion on Seconds Converter: within the C Programming forums, part of the General Programming Boards category; I want to convert seconds into hours, min and seconds. I was able to test it with a small equation ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    466

    Seconds Converter:

    I want to convert seconds into hours, min and seconds. I was able to test it with a small equation turning seconds into hours but now it returns zeros for every entry. I have tried several ways but I can't seem to figure it out. Can anyone point me in the right direction?

    Code:
    
    
    #include <stdio.h>
    
    
    void secCount(int *seconds, int *hours, int *min, int *sec);
    
    
    
    
    int main(int argc, const char * argv[])
    {
     int sec=0, hours=0, min=0, seconds =0;
    
        {
    
    printf("Enter the time in Seconds");
     scanf("%d",&seconds);
    
     secCount(&seconds, &hours, &min, &sec);
    
    printf("The time is equal to %d hours %d min and %d seconds", hours, min, sec);
    
        }
    
     return 0;
    
    }
    
    
    void secCount (int *seconds, int *hours, int *min, int *sec)
    
    
    {
     int days;
     int s = 0;
    
        days = *seconds / 86400; // sets total to hours //
        s %= 86400; // sends remainder //
        *hours = s / 3600; // sets total to min // 
        s /= 3600; // sends remainder //
        *min = s / 60; // gets min //
        *sec = s % 60;      // sets remanding to  sec // 
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,468
    Next time, please copy-paste your code as plain text, and with proper indentation, so it will be easier to read.
    Code:
    void secCount (int *seconds, int *hours, int *min, int *sec)
    {
        int s = 0;
        ...
        *hours = s / 3600;
    What is s? What is s / 3600? Why do you even have days or s as variables?

  3. #3
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Look at time.h for this. It has great constants and stuff to get you going.

    http://www.acm.uiuc.edu/webmonkeys/b...uide/2.15.html
    Last edited by HelpfulPerson; 07-10-2013 at 11:08 AM.

  4. #4
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    466
    There are 3600 seconds in an hour. I thought % send the remainder and therefore I would divide the remainder by 3600 to convert it into hours. Same patten follows for the rest of the code.

    As for plain text, sorry. I have been told that twice now and I am not to sure how to do it. Common sense tells me it is in word, but when I briefly looked for it I couldn't find it. My compiler uses the font above.
    I naturally know what it looks like, but I have never worked with it.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,468
    Quote Originally Posted by jocdrew21 View Post
    There are 3600 seconds in an hour. I thought % send the remainder and therefore I would divide the remainder by 3600 to convert it into hours. Same patten follows for the rest of the code.
    I should have said "What is the value of s? What is the value of s / 3600?". You have the right idea, but pay attention to what variables you are using. You set s to 0, then try to do calculations (divide and modulo) with it, expecting some non-zero answers for some reason. Perhaps you meant to use a different variable, or perhaps you meant to give s a meaningful value before doing the calculations. Note, I find it a bit easier to go the other direction:
    Code:
    *min = *sec / 60  // now min has the correct number of whole minutes (possibly over 60)
    *sec %= 60  // sec has the correct remainder of seconds
    // do likewise for minutes to hours, hours to days, etc, which will take care of having more than 60 minutes
    The thing I like about going in that order, is you only ever need to know the conversion factor between two adjacent units (i.e. 60 sec in a min, 24 hr in a day). No need to calculate things like 86400 sec in a day.


    Quote Originally Posted by jocdrew21 View Post
    As for plain text, sorry. I have been told that twice now and I am not to sure how to do it. Common sense tells me it is in word, but when I briefly looked for it I couldn't find it. My compiler uses the font above.
    My browser has a right-click option labelled "Paste As Plain Text". Perhaps your browser has the same? If not, open up a basic text editor, like notepad, and paste into an empty doc there (which will remove all formatting), then re-copy it from there (now without formatting), and paste into the forum.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Usually, your editor will have a Copy button or selection under Edit, from there, choose "As Plain Text", and you'll have it. If you use the default copy method, you'll keep the Word formatting, since Word will assume you are copying from one Word document, into another one, and will want to keep the formatting.

    I can't tell you the exact mouse clicks to make; I don't have Word here. But it's there.

    I thought % send the remainder and therefore I would divide the remainder by 3600 to convert it into hours. Same patten follows for the rest of the code.
    Yes, mod (%) does return the remainder.

    Two minutes with a few simple test seconds and a calculator, will confirm the right math to use. Great scott! that will be much faster than posting on a forum, and waiting for an answer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert seconds to hours, minutes and seconds
    By kkk in forum C Programming
    Replies: 2
    Last Post: 07-26-2011, 10:47 AM
  2. Time in seconds
    By udaraliyanage in forum C Programming
    Replies: 1
    Last Post: 04-09-2011, 07:21 AM
  3. Every x Seconds?
    By bobbinator in forum C++ Programming
    Replies: 14
    Last Post: 08-23-2009, 08:46 AM
  4. Super Pi at 1m done in under 10 seconds.
    By SlyMaelstrom in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 08-15-2006, 04:15 PM

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