Converting Time Program

This is a discussion on Converting Time Program within the C Programming forums, part of the General Programming Boards category; Hi, so I am making a program that hinges on a function that takes a total amount of seconds as ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    14

    Converting Time Program

    Hi, so I am making a program that hinges on a function that takes a total amount of seconds as input and is supposed to output using printf the number of days, hours, minutes, and seconds. The program is now able to compile, but I am not getting the output that I expected. Any advice?

    Code:
    #include <stdio.h>
    #include <math.h>
    #include "checkit.h"
    
    /* function declarations */
    
    /* time conversion function */
    
    
    void convertTime(int seconds);
    
    #define Sec_per_day 86400
    
    #define Sec_per_hr 3600
    
    #define Sec_per_min 60
    
    
    /* function definitions */
    
    
    void convertTime(int seconds){
    int Days;
    int Hrs;
    int Mins;
    int Secs;
    
    Days = seconds/Sec_per_day; /* int division returns # of days */
    
    
    Hrs = (seconds%Sec_per_day)/Sec_per_hr; /* left over seconds returns hrs */
    
    
    Mins = ((seconds%Sec_per_day)%Sec_per_hr)/Sec_per_min; /* left over seconds
                                                              returns mins */
    
    
    
    
    Secs = ((seconds%Sec_per_day)%Sec_per_hr)%Sec_per_min; /* left over seconds */
    
     printf("Elapsed time: %d days, %d hours, %d minutes, %d seconds\n",
    Days, Hrs, Mins, Secs);
    
     }
    Thanks.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    2
    what outputs are you getting? and with what inputs?

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    14
    Quote Originally Posted by xdelta View Post
    what outputs are you getting? and with what inputs?
    I am running the function in another program "time.c"

    Code:
    #include <stdio.h>
    #include "checkit.h"
    #include <math.h>
    #include "mylib.h"
    
    
    /* main */
    
    
    int main() {
    
    
    convertTime(90330);
    
    
    return 0;
    }
    The output is:

    real 0m0.000s
    user 0m0.000s
    sys 0m0.000s

    The output persists even after removing the print statement, leading me to believe it is coming from somewhere else in the function. I renamed all my variables to make sure I wasn't initiating some other kind of C function or command, but alas, I still have no progress at this point.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    2
    the program to which you're referring simply calculates the running time of your program. it has nothing to do with the actual unit conversion taking place. the code you posted appears to work fine. i tested it out with a few inputs and received the expected output. a program like this is simple enough that you shouldn't need to have so many external files and headers. A single main() function should do the trick (see below).

    Code:
    #include <stdio.h>
    
    #define SECS_PER_DAY  86400
    #define SECS_PER_HOUR 3600
    #define SECS_PER_MIN  60
    
    int main() {
    
           int days, hours, minutes, seconds;
    
           /* accept input from user, verify input was correctly received and stored */
           printf("\nEnter a number of seconds: ");
           scanf("%d", &seconds);
           printf("You entered %d seconds.\n", seconds);
    
           /* conversion steps */
           days = seconds / SECS_PER_DAY;
           hours = (seconds % SECS_PER_DAY) / SECS_PER_HOUR;
           minutes = ((seconds % SECS_PER_DAY) % SECS_PER_HOUR) / SECS_PER_MIN;
           seconds = ((seconds % SECS_PER_DAY) % SECS_PER_HOUR) % SECS_PER_MIN;
    
           /* print converted time units */
           printf("\n%d day(s), %d hour(s), %d minute(s), and %d second(s).\n", days, hours, minutes, seconds);
    }
    i'm not sure which platform you're running this on, or which compiler you're using. but if you simply compile (e.g. gcc timeconverter.c) and run (e.g. ./a.out) this program, it should work just fine. the output should look like this:

    Code:
    xdelta~/code gcc converter.c 
    xdelta~/code time ./a.out 
    
    Enter a number of seconds: 95000
    You entered 95000 seconds.
    
    1 day(s), 2 hour(s), 23 minute(s), and 20 second(s).
    
    real    0m3.310s
    user    0m0.000s
    sys     0m0.000s
    the last part of the output (real, user, sys, etc.) will only appear when you precede "./a.out" (or whatever you elect to name your executable) with "time" (assuming a linux environment). again, the time is unnecessary, unless you're required to include it for some other reason (perhaps a homework assignment).
    Last edited by xdelta; 01-18-2012 at 01:25 AM.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    14
    Thanks I appreciate the help. I think it was the fact that my program was labeled time.c and the executable I used was "time." I'm using putty, winscp, and cygwin terminal on my windows machine. We use a more ornate gcc compiler line for my class. Its gcc -ansi -Wall -Werror -lm.

    Frustrating as hell that the code itself works I just was unfortunate to use a name that was already taken by the C library.

    Anyways, everything is working perfectly now. Thanks again.
    Last edited by greeziakattack; 01-18-2012 at 01:31 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with time converting program.
    By brian75 in forum C++ Programming
    Replies: 2
    Last Post: 11-24-2009, 05:38 PM
  2. converting 64 bit time and date
    By pastitprogram in forum C++ Programming
    Replies: 29
    Last Post: 07-02-2008, 04:53 PM
  3. Converting Zulu Time to Current Time
    By Caldus in forum C++ Programming
    Replies: 3
    Last Post: 06-08-2006, 09:54 PM
  4. time converting
    By kumar14878 in forum C Programming
    Replies: 1
    Last Post: 04-29-2005, 02:04 AM
  5. Converting time
    By mepaco in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2002, 12:06 PM

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