Thread: Stuck on function exercise

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    Stuck on function exercise

    I've been learning C on my own from "C How to Program." There is this one practice exercise I'm having trouble wrapping my head around.

    It goes. Write a function that takes the time as three integer arguments(for hours, minutes, and seconds), and returns the number of seconds since the last time the clock "struck 12." Use this function to calculate the amount of time in seconds between two times, both of which are within one 12-hour cycle of the clock.

    My problem is whenever I input 0, 12, or 24 hours the program doesn't work. I've been at this for a couple days now any help would be greatly appreciated.
    Code:
    #include <stdio.h>
    
    long time(int, int, int);
    
    
    int main()
    {
    int h1, h2, m1, m2, s2, s1, sentinel = 1;
    
    while (sentinel == 1) {
      
       scanf("%d%d%d", &h1, &m1, &s1);
       if (h1 > 24 || h1 < 0 || m1 > 59 || m1 < 0 || s1 > 59 || s1 < 0) {
          printf("Time is out of range\n");
          continue;
          }
    
       scanf("%d%d%d", &h2, &m2, &s2);
       if (h2 > 24 || h2 < 0 || m2 > 59 || m2 < 0 || s2 > 59 || s2 < 0) {
          printf("Time is out of range\n");
          continue;
          }
    /* ------------ Why isn't the following if statement allowed?---------*/
       if ( !( h2 >= 12 && h1 >= 12 ||  h2 <= 12 && h1 <= 12 ) ) {
          printf("The two times must be in the same twelve hour cycle\n");
          continue;
          }
    
       if ( time(h1, m1, s1) > time(h2, m2, s2) ) {
          /*  Test statement  */
          printf("time1: %d  time2: %d", time(h1, m1, s1), time(h2, m2, s2) );
    
          printf("%d\n", time(h1, m1, s1) - time(h2, m2, s2) );
          }
       else {
          /*  Test Statement  */
          printf("time1: %d   time2: %d", time(h1, m1, s1), time(h2, m2, s2) );
    
          printf("%d\n", time(h2, m2, s2) - time(h1, m1, s1) );
          }
    }
    
    return 0;
    }
    
    
    long time(int hours, int minutes, int seconds)
    {
       return hours % 12 * 3600 + minutes * 60 + seconds;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What do you mean "not allowed"?
    When I compile it, I get
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function `main':
    foo.c:24: warning: suggest parentheses around && within ||
    foo.c:31: warning: int format, long int arg (arg 2)
    foo.c:31: warning: int format, long int arg (arg 3)
    foo.c:33: warning: int format, long int arg (arg 2)
    foo.c:37: warning: int format, long int arg (arg 2)
    foo.c:37: warning: int format, long int arg (arg 3)
    foo.c:39: warning: int format, long int arg (arg 2)
    All the "int format" warnings are telling you that your "%d" should be "%ld" to match the types of the values you're printing.

    The "suggest parentheses" is telling you that programmers often mis-understand the precedence of these operators, and that using parentheses will clear it up
    if ( !( (h2 >= 12 && h1 >= 12) || (h2 <= 12 && h1 <= 12) ) )
    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.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    long time(int hours, int minutes, int seconds)
    {
       return hours &#37; 12 * 3600 + minutes * 60 + seconds;
    }
    Although you're not using time.h, this function name conflicts with that of the standard C lib time() implementation. I would suggest changing it to avoid confusion and to future-proof your code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  2. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 10:08 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM