Time to seconds program

This is a discussion on Time to seconds program within the C Programming forums, part of the General Programming Boards category; This is what i gotta do: Create a C function called SecondsSinceMidnight() that has 3 parameters of type "long". The ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    24

    Time to seconds program

    This is what i gotta do:

    Create a C function called SecondsSinceMidnight() that has 3 parameters of type "long". The 3 parameters will be: "hours", "minutes" and "seconds". This function will use the 3 arguments passed to it to calculate and return (as a type "long" value) the number of seconds since the clock "struck midnight". The "hours" value passed to the function will be in military time (0 to 23). For example, if the input to the function (the value of the arguments passed to the function)is 15 hours, 5 minutes and 10 seconds, the function should return (15 * 60 * 60) + (5 * 60) + 10 = 54310 seconds.

    Have your main() function input from the user two sets of time values. Have your main() program output the two sets of input times. Make use of the SecondsSinceMidnight() function to calculate the "seconds since midnight" for each of the two sets of input times. The main() function should then output the two results returned by the function (this means that you will need to invoke the SecondsSinceMidnight() function twice in your main() function). The main() function should then calculate the "difference in seconds" between the two values returned by the SecondsSinceMidnight() function and output the result. The "difference in seconds" should always be output as a positive number (you are allowed to use abs() -- the absolute value library function).

    When you execute my .exe file, test it with the following 3 sets of data:

    Data Set 1:

    Enter the first hour value(0-23): 23
    Enter the first minute value(0-59): 59
    Enter the first second value(0-59): 59

    Enter the second hour value(0-23): 0
    Enter the second minute value(0-59): 0
    Enter the second second value(0-59): 0

    Data Set 2:

    Enter the first hour value(0-23): 12
    Enter the first minute value(0-59): 15
    Enter the first second value(0-59): 30

    Enter the second hour value(0-23): 12
    Enter the second minute value(0-59): 15
    Enter the second second value(0-59): 31

    Data Set 3:

    Enter the first hour value(0-23): 17
    Enter the first minute value(0-59): 15
    Enter the first second value(0-59): 10

    Enter the second hour value(0-23): 5
    Enter the second minute value(0-59): 0
    Enter the second second value(0-59): 45


    Code:
    #include <stdio.h>
    
    long SecondsSincemidnight(long hours, long minutes, long seconds);
    int main()
    	{
    	long hours;
    	long minutes;
    	long seconds;
    	long hours2;
    	long minutes2;
    	long seconds2;
    	long variable1;
    	long variable2;
    	long sum=0;
    	
    	do{
    		printf("\nEnter the first hour value(0-23): ");
    		scanf("%ld", &hours);
    		}while(hours>23);
    	do{
    		printf("Enter the first minute value(0-59): ");
    		scanf("%ld", &minutes);
    		}while(minutes>59);
    	do{
    		printf("Enter the first second value(0-59): ");
    		scanf("%ld", &seconds);
    		}while(seconds>59);
    		
    	do{
    		printf("\nEnter the second hour value(0-23): ");
    		scanf("%ld", &hours2);
    		}while(hours2>23);
    	do{
    		printf("Enter the second minute value(0-59): ");
    		scanf("%ld", &minutes2);
    		}while(minutes2>59);
    	do{
    		printf("Enter the first second value(0-59): ");
    		scanf("%ld", &seconds2);
    		}while(seconds2>59);
    	
    	variable1 = SecondsSincemidnight(hours, minutes,  seconds);
    	printf("\nThe first input time converted into seconds is: %ld", variable1);
    	
    	variable2 = SecondsSincemidnight(hours2, minutes2,  seconds2);
    	printf("\nThe second input time converted into seconds is: %ld\n", variable2);
    
    	sum= variable1- variable2;
    	
    	if(sum<0)
    		sum=-(sum);
    	printf("\nThe difference in seconds between the two input time is: %ld\n", sum);
    		
    	return  0;
    	}
    
    long SecondsSincemidnight(long a, long b, long c)
    	{
    	return a*3600+b*60+c;
    	}

    I cant seem to get it to say try again when u enter a negative #, also does this look alrite er can it be simplified

    thx

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    do{
    		printf("\nEnter the first hour value(0-23): ");
    		scanf("%ld", &hours);
    		}while(hours>23);
    How about:

    Code:
    do{
        printf("\nEnter the first hour value(0-23): ");
        scanf("%ld", &hours);
    } while(hours>23 && hours<0);
    Is this what you mean?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with time program
    By rushhour in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2009, 07:39 AM
  2. Replies: 11
    Last Post: 06-27-2008, 03:39 PM
  3. How to restrict a program in time consumed
    By gogoc in forum C++ Programming
    Replies: 3
    Last Post: 05-19-2008, 11:10 AM
  4. Need help with time
    By Gong in forum C++ Programming
    Replies: 7
    Last Post: 01-11-2007, 01:43 PM
  5. write a program that will set the time to 5:58
    By jupimako in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2003, 02:51 AM

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