hour,minutes & seconds

This is a discussion on hour,minutes & seconds within the C Programming forums, part of the General Programming Boards category; Hi. Whats wrong here? Code: hour = total_sec /3600; min = total_sec % 60; sec = min % 60;...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    16

    hour,minutes & seconds

    Hi. Whats wrong here?
    Code:
    hour = total_sec /3600;
      min = total_sec % 60;
      sec = min % 60;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your calculation of min doesn't really bear any resemblance to what a minute is.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    Say you're given an integer 123, and your task is to extract each digit using /10 and %10

    Well it's exactly the same, except hms uses /60 and %60
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    16
    explain please? I thought 3600/60= 60.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It is. Is that relevant? No. (EDIT: A hint, because I'm feeling generous. 120 seconds is 2 minutes. What is 120%60?)
    Last edited by tabstop; 05-26-2011 at 11:31 PM.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    16
    Yes.true & it was working for me when I was just doing minutes & seconds, but when I included hours it didn't.like so
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int input_v,hour,min,sec;
    
    	printf("input number in total seconds\n");
    	scanf("%d", &input_v);
    	while(input_v > 3600){
    	hour = input_v /3600;
    	min = input_v % 60;
    	sec = min % 60;
    	printf("%dhours%dminutes and %dseconds\n",hour,min,sec);
    	}	
    	min = input_v / 60;
    	sec = input_v % 60;
    	printf("%dminutes and %dseconds\n",min,sec);
    	return 0;
    }
    Last edited by anaer0bic; 05-26-2011 at 11:38 PM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's because you did something completely different (and, coincidentally, correct). Compare your correct version on line 15 with your not-at-all correct version on line 11.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    16

    Smile

    cheers tabstop

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    16
    So this works with the while statement commented out. Whats wrong with the while?
    Code:
    #include <stdio.h>
    int main()
    {
     int input_v,hour,min,sec;
    
     printf("input total seconds\n");
     scanf("%d", &input_v);
     hour=input_v/3600;
     min=(input_v%3600)/60;
     sec=(input_v%60)%60;
     /*while(hour<2){
     printf"%dhour %dminutes %dseconds\n",hour,min,sec);
     }*/
     printf("%dhours %dminutes %dseconds\n",hour,min,sec);
    	
    	
     return 0;
    }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    How is hour going to change inside the loop? If you get in, you can never get out. You probably meant "if" instead.

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    2
    Hey guys. I'm new here and new on C.

    Im also trying to convert seconds to hours, minutes, seconds.

    I got this but i cant seem to make it work. I can compile it but it exits there's really something wrong. Can anyone point out the problem or can give a hint.

    Code:
    #include <stdio.h>
    
    main( )
    
    {
          int input, hr, min, sec;
          
          printf("Input Value: ");
          scanf ("%d", &input);
          hr = (input/3600);
          min =(input%3600)/60;
          sec = (input%60)%60;  
          printf ("%d Hours, %d Minutes, %d seconds", hr, min, sec);
          }
    Thanks for any reply.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Once a program is finished, it always exits; that's just the way programs work. So once it prints the Hours, Minutes, and Seconds, there's nothing left for it to do, so it stops. You should be running this program from the console anyway so that you can see the output.

    (Also: This thread wasn't extremely dead, but in general you make a new thread and maybe make a link to the old one if you need to refer to it. In this case you don't refer to anything that comes before, so putting it in an old thread isn't useful.)

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Couple of preliminary things:

    1) If you have a new question, you might as well start a new thread. If you think it relates substantially to some other thread, add a link.

    2) the return type of main is int, and it should do so:

    Code:
    int main (void) {
         return 0;
    }

    Beyond that, your code is fine, I ran it, it compiles without errors (but there is a warning about the return type of main) and it works. I suspect you are working on MS Windows, and by "exit" you mean the window disappears. Add something like this at the end:

    Code:
    	int c = 0;
    	while (c != 'q') c = getchar();
    Now you have to enter q (and return) to end the program.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,496
    Read the forum guidelines. You shouldn't be posting to a thread more than two weeks old. Make your own next time.

    It's int main(void), and return an int at the end, usually 0 for success.

    Lastly, your code works, apart from the int main thing. There's not really something wrong, you just need to keep the console window open until you're done. Read this: Cprogramming.com FAQ > Stop my Windows Console from disappearing everytime I run my program?

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    2
    Thank you tabstop, MK27 and anduril462.

    I'm so stupid. I didn't know about that console disappearing. Thank you for the inputs it was an enlightenment

    I'm sorry about bumping the topic. I'll be reading the guidelines before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 10-05-2009, 12:19 AM
  2. function returning hour in either 12 or 24 hour format
    By stanlvw in forum C Programming
    Replies: 4
    Last Post: 01-01-2008, 05:02 AM
  3. Roidian: Final Hour (72 hour gd compo results)
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 12-29-2004, 09:27 PM
  4. Algo for create hours and minutes with given seconds
    By BianConiglio in forum C Programming
    Replies: 6
    Last Post: 05-30-2004, 06:50 PM
  5. Converting MInutes to Hours and MInutes
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-30-2001, 08:07 PM

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