Any assistance would be greatly appreciated

This is a discussion on Any assistance would be greatly appreciated within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #define MINS_HR 60 //define minutes per hour #define SECS_MIN 60 //define seconds per minute int main(void) { ...

  1. #16
    Registered User
    Join Date
    Feb 2006
    Posts
    32
    Code:
    #include <stdio.h>
    
    #define MINS_HR 60 //define minutes per hour
    #define SECS_MIN 60 //define seconds per minute
    
    int main(void)	{
    
    	int total;
    	int h, m, s;
    
    	printf("How many seconds whould you like to convert?\n");
    	scanf("%d", &total);
    	
    	h = (total / (MINS_HR * SECS_MIN));
    	if (h >= 1)	{
    		total = total - (MINS_HR * SECS_MIN);
    		if (h <= 0)	{
    			h = 0;
    		}
    	}
    	
    	m = (total / SECS_MIN);
    	if (m >= 1)	{
    		total = (total - (SECS_MIN * m));
    			if (m <= 0)	{
    				m = 0;
    			}
    	}
    
    	s = total;
    	printf("%d:%d:%d", h, m, s);
    }
    this program is supposed to convert seconds to hours:minutes:seconds...for example, 4000 seconds would be 01:06:40.

    is this the most efficient way? i've noticed that it doesn't work for all inputs. higher amount of seconds, the less accurate the output is. please advise.

  2. #17
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    This is a lot easier if you also use the % modulo operator.
    h = total / 3600;
    ms = total % 3600;
    m = ms / 60;
    s = ms % 60;

  3. #18
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Code:
    		total = total - (MINS_HR * SECS_MIN);
    shouldn't this be:

    Code:
    		total = total - (MINS_HR * SECS_MIN * h);

  4. #19
    Registered User
    Join Date
    Feb 2006
    Posts
    32
    haha, oh man you don't know how much i appreciate you guys

    thanks so much,
    michael

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assistance greatly needed to do LCD Display
    By HELPMEPLEASE!!! in forum C Programming
    Replies: 6
    Last Post: 03-28-2009, 05:07 PM
  2. Noobie question, all help greatly appreciated!
    By axehero in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2007, 09:47 PM
  3. Replies: 2
    Last Post: 05-10-2006, 10:43 PM
  4. Simple question. Help will be appreciated.
    By wickedclownz in forum C++ Programming
    Replies: 2
    Last Post: 06-19-2003, 02:18 AM
  5. Your help would be greatly appreciated
    By Sway2 in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2002, 06:55 AM

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