Need Help writting code for telling time!!

I need write a code with a function that returns the number of seconds since the last time the clock struck 12. Im really new to C++ and I want to see how to do this and can really use the help! Please and Thank You!!


1. Write a function that takes in three integer parameters (representing hours, minutes, and seconds) and returns the number of seconds since the last time the clock "struck 12" (i.e. was at 12:00:00 -- AM or PM doesn't matter).

2. To test this function, write a main() routine (in the same file) that prompts the user to enter hours, minutes, and seconds for two different clock times; then uses the Seconds function to calculate the shortest amount of time in seconds between the two times (both of which are within one 12-hour cycle of the clock); the print out the number of seconds since "striking 12" for both clocks, as well as the number of seconds between the two clock times.

What didn't you understand here: http://www.cplusplus.com/forum/beginner/134579/
closed account (jvqpDjzh)
Maybe also this time there might be bugs, I am tired, it's 3.42 here (good excuse).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int SecondsSinceLast12(int h, int m, int s)
{
    if(h < 0 || m < 0 || s < 0) return -1;
    else
    {
        if(s > 59)
        {
            m += s / 60;
            s = s % 60;
        }
        if(m > 59)
        {
            h += m / 60;
            m = m % 60;
        }
        if(h > 12) h = h % 12;
        return h*60*60 + m*60 + s;
    }
}


int DifferenceBetweenTimes(int time_in_secs1, int time_in_secs2)
{
    return abs(time_in_secs1 - time_in_secs2);
}
Last edited on
Topic archived. No new replies allowed.