Thread: How to add hours:minutes:seconds (hh:mm:ss) keeping output values <60

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    15

    How to add hours:minutes:seconds (hh:mm:ss) keeping output values <60

    the official assignment:
    "DIRECTIONS: Write a C program which prompts the user
    for two times specified in the format hh:mm:ss
    (hours:minutes:seconds). Your program should
    compute and display the sum of the two times as specified
    in the examples below. In the result, hours <= 23, minutes <=59,
    seconds <= 59. The output should be followed immediately by
    'PROGRAM ENDS', followed by a single newline character."

    i.e. if a user enters

    11:45:34 and
    09:28:41

    then the output should be
    21:14:15

    NOT

    20:73:75

    I'm sure the modulus operator is involved in this somehow. While our teacher didn't say we couldn't use if:else statements, how would you do this without them?

    I tried searching but couldn't find what I was looking for.

    and if it matters, we do everything via linux terminal
    Last edited by pinkfloyd4ever; 10-04-2009 at 07:13 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I would use the modulus operator (and the division operator), which does not require using if in any way.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    15
    hmm, how exactly? Obviously I want to simply add the seconds (or minutes) if the sum of the 2 input values is less than 60, but how do I account for sums greater than 60 without the if:els statement. Sorry I'm a newb.

    I currently have the following variables declared: h1, m1, s1 (for 1st user inputted hours:minutes:seconds) h2, m2, s2 (for second user input) and h3, m3, and s3 for the summed output.

    does 59%60 equal 0 or 59?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    % gives the remainder (and / gives the quotient). Since you will do the exact same thing whether the sum is smaller than 60 or bigger than 60 (use mod to find the remainder and add the carry to the next "unit"), the use of an if is not needed.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    15
    right, I know what the modulus and division operators do.

    So I would say
    Code:
    s3=((s1+s2)%60);
    m3=(m1+m2+(s1+s2)/60)%60;
    this is what I have so far. When I run it, it prompts me for the first time but it skips the second one (prints "TIME2# " but doesn't prompt for input), prints "PROGRAM ENDS" and the program ends. Can anyone help?

    Code:
         1  #include<stdio.h>
         2
         3  /*
         4  [name]
         5  [class #]
         6  10-04-09
         7  pe2.c
         8  */
         9
        10  int main(void)
        11  {
        12  int h1, m1, s1, h2, m2, s2, h3, m3, s3;
        13  printf("TIME1# ");
        14  scanf("%d%d%d", &h1, &m1, &s1);
        15  printf("TIME2# ");
        16  scanf("%d%d%d", &h2, &m2, &s2);
        17  m3=((s1+s2)%60);
        18  printf("%d", m3);
        19  printf("PROGRAM ENDS\n");
        20  return 0;
        21  }
    Last edited by pinkfloyd4ever; 10-04-2009 at 08:50 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Make sure you don't type any punctuation, like : or similar, since you have told the computer (via the format string %d%d%d) that there isn't any.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    15
    ohhhhhh. It's supposed to be written for the entry format
    "hh:mm:ss"

    so i guess I need to change the format string to "%d:%d:%d", huh?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That would be correct.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    15
    ok, so I'm just trying to get this to at least do something that makes sense, so I currently have
    Code:
         1  #include<stdio.h>
         2
         3  /*
         4  myname
         5  classsection
         6  10-04-09
         7  pe2.c
         8  */
         9
        10  int main(void)
        11  {
        12  int h1, m1, s1, h2, m2, s2, h3, m3, s3;
        13  printf("TIME1# ");
        14  scanf("%d:%d:%d", &h1, &m1, &s1);
        15  printf("TIME2# ");
        16  scanf("%d:%d:%d", &h2, &m2, &s2);
        17  s3=(s1+s2);
        18  m3=(m1+m2);
        19  h3=(h1+h2);
        20  printf("%d:%d:%d", &h3, &m3, &s3);
        21  printf("PROGRAM ENDS\n");
        22  return 0;
        23  }
    I know this won't keep the output minute and second values under 60 & carry the remainders, but again I'm just trying to get it to do something correctly for now.

    when I compile I get these warnings
    Code:
    pe2.c: In function `main':
    pe2.c:20: warning: int format, pointer arg (arg 2)
    pe2.c:20: warning: int format, pointer arg (arg 3)
    pe2.c:20: warning: int format, pointer arg (arg 4)
    and now when I run it I at least can input both values now, but this is what I get (when I enter 12:12:34 and 34:12:73 obviously)
    Code:
    TIME1# 12:12:34
    TIME2# 34:12:73
    -4195092:-4195096:-4195100PROGRAM ENDS
    what am I doing wrong in line 20?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    printf and scanf are not the same thing, and do not require the same arguments. As your compiler is telling you, %d is an int format and so an int is expected (not a pointer).

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    15
    ahh, right. Thanks tab

    goddamn, using SSH Secure Shell is such a PITA!! And ytf are we even learning C!?! Who actually uses C anymore...ugh
    anyway, I've got it to compile w/o warnings and I have it adding, modulusing (is that a word?)& carrying correctly, but my class's automated turnin site still doesn't like it. I believe it's because when the sum of the seconds or minutes is a single digit number, it returns a single digit number, ie "x" not "0x"

    For example, when I enter
    12:34:52 and
    09:37:11

    it returns
    22:12:3

    whereas the teacher wants it to return
    22:12:03

    how would I make it put a "0" in front of single digit sums in the output?

    here's where I'm at
    Code:
         1  #include<stdio.h>
         2
         3  /*
         4  .........
         5  ........
         6  10-04-09
         7  pe2.c
         8  */
         9
        10  int main(void)
        11  {
        12  int h1, m1, s1, h2, m2, s2, h3, m3, s3;
        13  printf("TIME1# ");
        14  scanf("%d:%d:%d", &h1, &m1, &s1);
        15  printf("TIME2# ");
        16  scanf("%d:%d:%d", &h2, &m2, &s2);
        17  s3=(s1+s2)%60;
        18  m3=(m1+m2+((s1+s2)/60))%60;
        19  h3=(h1+h2+((m1+m2)/60));
        20  printf("%d:%d:%d", h3, m3, s3);
        21  printf("\nPROGRAM ENDS\n");
        22  return 0;
        23  }
    thanks again for your help thus far, tabstop
    Last edited by pinkfloyd4ever; 10-04-2009 at 11:35 PM.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did you ever bother reading the manual on printf and how to pad with 0s?

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    15
    ahh right, rtfm

    sonofab!tch

    Code:
    #include<stdio.h>
    
    /*
    Kevin Gentsch
    CS1250-001
    10-04-09
    pe2.c
    */
    
    main(void)
    {
    int h1, m1, s1, h2, m2, s2, h3, m3, s3;
    printf("TIME1# ");
    scanf("%d:%d:%d", &h1, &m1, &s1);
    printf("TIME2# ");
    scanf("%d:%d:%d", &h2, &m2, &s2);
    s3=(s1+s2)%60;
    m3=(m1+m2+((s1+s2)/60))%60;
    h3=(h1+h2+((m1+m2)/60));
    printf("%2.2d:%2.2d:%2.2d\n", h3, m3, s3);
    printf("PROGRAM ENDS\n");
    return 0;
    }
    I've got it working perfectly (or so it seems) but the turnin site still doesn't like it. Guess I'll have to talk to the teacher

    thanks tho tabstop, you rock

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble making a function do what I want.
    By Shamino in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2007, 11:20 AM
  2. Connecting input iterator to output iterator
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 02:18 AM
  3. Zero Values being ignored for output (hexadecimal)
    By Oldman47 in forum C++ Programming
    Replies: 5
    Last Post: 12-12-2006, 04:01 PM
  4. Replies: 1
    Last Post: 05-15-2006, 04:55 PM
  5. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM