Thread: Time converting problem. (24-hour military format to minutes)

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    1

    Time converting problem. (24-hour military format to minutes)

    Greetings, I'm a student who's currently in his first year and first semester in his CScourse and am having some issues with this program right here.

    Okay, so you have to enter two different times in 24-hour format so the program could calculate the total
    amount of time spent and then convert that time into minutes.

    i.e. 100(start time) and 300(end time)


    here's my shot at it:


    Code:
    #include <conio.h>
    #include <stdio.h>
    
    
    void main(){
    
    
    	int start_time;
    	int end_time;
    	int semi_time;
    	double final_time;
    
    
    	printf("Please enter the time you started (in military time): \n");
    	scanf("%d", &start_time);
    	printf("Please enter the time you ended (in military time): \n");
    	scanf("%d", &end_time);
    
    	semi_time = end_time - start_time;
    	final_time = semi_time * 0.60;
    
    
    	printf("The time that you have spent is: %.0f minute(s)\n", final_time);
    
    
    	getch();
    
    
    }


    Now the problem is, when I enter certain amounts of time, the output comes out
    wrong.

    When I enter "120" and "200" the output that comes out is "48 minutes", which is obviously incorrect, becauseit should be "40 minutes".

    Now, I know I this is the part that I got wrong:

    semi_time = end_time - start_time;
    final_time = semi_time * 0.60;



    But I can't figure out what to do with it to save my life.
    Any help would be greatly appreciated. Thanks in advance.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You must convert both input values into the same units before the time difference subtraction.
    I suggest in most problem to convert the input into the smallest unit; I would guess that would be minutes.

    How do you convert "120" into the correct amount of minutes?
    Write down the steps.
    Convert the steps into C code.

    Tim S.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Military Time

    I think it's easier if you just convert to regular time and then find the difference.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Get your first time... 11:17
    What minute of the day is it? (How many minutes since midnight?)
    Get your second time... 22:31
    What minute of the day is that?

    Subtract.

    Of course you're going to run into all kinds of complications... like when you pass midnight and the second number is smaller than the first... but that should get you started in the right direction...

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by whiteflags View Post
    Military Time

    I think it's easier if you just convert to regular time and then find the difference.
    Actually I've always found military time easier for these problems...

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Additionally:
    1. void main is wrong. Read How to define main()-FAQ
    2. getch() is compiler specific. If you are going to get into programming, you are going to want to learn how to write portable code. I would suggest using something defined by the standard, such as getchar().
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hour,minutes & seconds
    By anaer0bic in forum C Programming
    Replies: 14
    Last Post: 06-28-2011, 11:19 AM
  2. Replies: 7
    Last Post: 11-21-2009, 01:02 AM
  3. function returning hour in either 12 or 24 hour format
    By stanlvw in forum C Programming
    Replies: 4
    Last Post: 01-01-2008, 06:02 AM
  4. Military Time Functions
    By BB18 in forum C Programming
    Replies: 6
    Last Post: 10-10-2004, 01:57 PM
  5. Converting MInutes to Hours and MInutes
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-30-2001, 08:07 PM

Tags for this Thread