Thread: Time Calculating

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    India
    Posts
    14

    Time Calculating

    Hi! I am a bigner and learning the C programming I am having a problem in Converting the Seconds (Input by User) into Days, Hours, Minuts & Seconds.
    Can anyone help me. I am using the codes like this


    long t_ime_s, d,h,m,s;
    printf("Enter the time in seconds");
    scanf("%ld", &t_ime_s);

    m=s/60;
    h=m/60;
    d=h/60;
    printf("Total days %ld",d);
    Last edited by kumar14878; 04-23-2005 at 05:37 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    s was never set to anything t_ime_s contains the number of seconds entered by the user so:
    Code:
    m=t_ime_s/60;
    h=m/60;
    d=h/24;//there isn't 60 hours in a day
    This isn't so accurate as it rounds to the nearest minute/hour/day
    Last edited by Quantum1024; 04-23-2005 at 05:43 AM.

  3. #3
    Widdle Coding Peon Aerie's Avatar
    Join Date
    Dec 2004
    Posts
    115
    First, USE CODE TAGS. Put [ code ] in front of any code you type, and [ /code ] (both without the spaces) after it. This makes it more readable and is good manners.

    Second, you're dividing number of hours by 60 to get number of days.

    Third, you're inputting a value in seconds into t_ime_s, but you never do anything with it after that. s has NO MEANINGFUL VALUE until you do something with it, like set it to equal t_ime_s. Unless you assign them a value, ALL non-static variables WILL contain garbage.

    Fourth, you don't need the variable t_ime_s at all. Just do
    Code:
    scanf("%ld", &s);
    to put the number of seconds into s, then
    Code:
    m = s / 60;
    h = m / 60;
    d = h / 24;
    will make sense.

    Of course, you could just convert the number of seconds to days in one line, using something like
    Code:
    d = (((s / 60) / 60) / 24);
    and then print off the value of d; this would be better unless you need the number of minutes and hours later.

    You also should use floats. I'm not sure what "long" defaults to, but long is just a modifier for other variable types, like float or int... or just regular int, if you want whole number values.

    I'd suggest using regular floats; they provide plenty of precision for your needs. I also suggest formatting the output with %0.2f instead of just %f, so only 2 decimal places will be output, unless you really WANT the extra precision.

    The finished code, in this case, would look something like this, assuming you didn't need the number of hours and minutes and wanted to limit the decimal precision:

    Code:
    float s, d;
    printf("Please enter the number of seconds and press Enter.\n");
    scanf("%f", &s);
    d = (((s / 60) /60) / 24);
    printf("%0.2f seconds is equal to %0.2f days.\n", s, d);
    Last edited by Aerie; 04-23-2005 at 06:13 AM. Reason: Typos galore!
    I live in a giant bucket.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  2. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 03:41 AM
  3. Replies: 3
    Last Post: 11-15-2003, 11:20 AM
  4. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM