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;
}