Thread: How do I make difference between hours and hour (beginner)

  1. #1
    Fedryan
    Guest

    How do I make difference between hours and hour (beginner)

    Hi! - I'm only a beginner, but I need some help with this problem.

    I'm writing a program that converts seconds to hours and minutes - That works fine! But if I write:
    3700 seconds
    It answers
    That equals to: 1 hours, 1 minutes and 40 seconds.

    I now want my program to write
    1 hour, 1 minute and 40 seconds.

    And if there is no hours at all it should only write
    400 seconds equals 6 minutes and 40 seconds.

    To sum up: The program shall write hour/hours minute/minutes second/seconds correct.

    I've made one succesfull program, but it was written with 27 "if-statements" and I know there is a better way.

    Code:
    #include <stdio.h>
    
    int
    main(void)
    {
    
      int sec, secs, minute, hour, rest;
    
        printf("Type in the number of seconds:");
        scanf("%d", &sec);
    
        hour = sec/3600;
        rest = sec%3600;
        minute = rest/60;
        secs = rest%60;
        
        printf("That equals to %d hours, %d minutes og %d seconds. \n", hour, minute, secs);
    
    return (0);
    }
    So...

    Any good advice on how to make the program know when to write hour or hours?

    /Fedryan

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You always print "hour" if there are one or more hours to be printed. So "hour" is the base word.

    Let's call the int variable for this, "hour". So:

    Code:
    char plural='s';
    
    if(hour>1) 
       printf("%d hours",hour);
    else if(hour == 1)
       printf("%d hour",hour);
    
    //and when done printing up seconds, be sure to
    //add a newline to the end of the print statement.
    
    //which can fit into a single line, using the compact 
    //version of the above:
    
    for(i=0;i<5;i++)
       printf("%d hour%c\n",i,ch=i==1?' ':'s');
    The empty char is actually a space between the two single quotation marks. Otherwise it's an error, but it fits perfectly as the space between words. Run the above for loop, and you'll see how it works.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    Code:
    #include <stdio.h>
    Code:
    int
    main(void)
    {
    
      int sec, secs, minute, hour, rest;
    
        printf("Type in the number of seconds:");
        scanf("%d", &sec);
    
        hour = sec/3600;
        rest = sec%3600;
        minute = rest/60;
        secs = rest%60;
         
    if(hour > 1)
        printf("That equals to %d hours, %d minutes og %d seconds. \n", hour, minute, secs);
    else if (hour == 1)
        printf("That equals to %d hour, %d minutes og %d seconds. \n", hour, minute, secs);
    else 
        printf("That equals to minutes og %d seconds. \n", minute, secs);
    
    
    
    
    return (0);
    
    



    not too sure though

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Try this
    Code:
    printf("That equals to %d hour%s\n", hour, hour == 1 ? "" : "s" );
    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.

  5. #5
    Fedryan
    Guest
    Quote Originally Posted by Salem View Post
    Try this
    Code:
    printf("That equals to %d hour%s\n", hour, hour == 1 ? "" : "s" );
    Thanks, that was smart - But when there is "0 hour" it should not write anything at all, and this still does write "0 hour"

    @Africanwizz, that is what I've allready done in my own try, but to cover all possible outcomes, you will have to write 27 lines (3x3x3), of "if-statements" because it is either hours, hour or nothing at all - minutes, minute or nothing at all - seconds, second or nothing at all.

    @Adak That looks like a great idea! However, I do not understand what you do on the last lines?

    "for(i=0;i<5;i++)" - Can you explain this?

    Thanks for all your input

    /Fedryan

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > Thanks, that was smart - But when there is "0 hour" it should not write anything at all, and this still does write "0 hour"
    So apply a bit more logic to the problem then.
    Like for example printing hours, minutes and seconds with different print calls, and some if statements.
    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.

  7. #7
    Fedryan
    Guest
    Yeah, now I have written it like:

    Code:
    printf("That equals to");
    if (hour >= 1)
    printf(" %d hour%s", hour, hour == 1 ? "" : "s" );
    if (minute >= 1)
    printf(" %d minute%s", minute, minute == 1 ? "" : "s" );
    if (secs >= 1)
    printf(" og %d second%s\n", second, second == 1 ? "" : "s" );
    This gives me the result on one line, and works perfectly!

    My only problem (and it is a minor one), is that when it ends on 0 seconds, i dont get the newline (\n) :-/
    For example if I enter 9000 seconds, it will answer:

    That equals to 2 hours 30 minutes$

    And I dont get the \n and therefore $-sign in the ending. Any suggestions?
    Last edited by Fedryan; 10-06-2013 at 08:34 AM.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The last loop in my post just printed out:

    0 hours
    1 hour
    2 hours
    3 hours
    4 hours

    Showing that it handled plural hours correctly, even for the zero hours.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > And I dont get the \n and therefore $-sign in the ending. Any suggestions?
    Print the newline separately, and unconditionally.
    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.

  10. #10
    Fedryan
    Guest
    Quote Originally Posted by Salem View Post
    > And I dont get the \n and therefore $-sign in the ending. Any suggestions?
    Print the newline separately, and unconditionally.
    Ofcouse! Why didn't I think of that?

    Thank you so much for the help! - My program runs almost perfect now. Now all I need; is a way to make it write "and" when there is more than two. For example:

    That equals to 1 hour and 30 minutes and 2 seconds.
    But if I write and in the the printf-line, I'm afraid that if there is 0 hours it will write:

    That equals to and 30 minutes and 2 seconds (example)
    - Which sounds stupid

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The phrase "that equals to" is incorrect grammar. Correct would be "that is equal to", but that has a formal tone that doesn't sound modern. "that equals:" would be a better choice. Another choice would be the more concise "equals:" where the subject is implied.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. 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, 10:27 PM
  3. Difference b/ween hours into minutes??
    By CodeFuse in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2003, 02:01 AM
  4. What A Difference 30 Years Make...
    By -dcx- in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-18-2002, 06:38 PM
  5. gmake or make, what's the difference?
    By Hubas in forum C++ Programming
    Replies: 0
    Last Post: 02-13-2002, 04:42 AM

Tags for this Thread