12 Hour Clock Displays Incorrectly after 11:59:59

I have program that outputs a 12 & 24 hour Clock side by side with a users options menu to allow user to add an hour, a minute, and a second to both clocks. The issue I am having is that when the hour reaches 12 the standard clock looks like the 24 hour format. That is, instead of it displaying the time as 12:36:45 AM it displays 00:36:45 AM. If the clock reads 00:36:45 and then I choose to add one hour it displays correctly 01:36:45 AM. I have only provided one section of my code. I have tried researching the issue and attempted to fix it on my own and have had no luck. If

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
public:
    _Standard_Time(int hr, int min, int sec)
    {
        // if value of hr is greater than 12, reset it from 1

        hour = hr > 12 ? hr % 12 : hr;
        minute = min;
        second = sec;

        // if value of hr is less than 12, make it AM, otherwise PM

        am_pm = hr < 12 ? AM : PM;  // C26812 reported here (enum type 'AM_PM' is unscoped) 

    }

    // Methods for Standard Time

    void addOneHour()   
    {
        if (hour == 11)  //FIX ME //if its currently 11:something 
        {

            hour = 0;  //Set hour to 00:something and flip AM PM ?

            am_pm = am_pm == AM ? PM : AM;
        }
        else {
            hour += 1;
        }
    }

   
    void addOneMinute()
    {
        if (minute == 59) {
            minute = 0;
            addOneHour();
        }
        else {
            minute += 1;
        }
    }

    void addOneSecond()
    {
        if (second == 59) {
            second = 0;
            addOneMinute();
        }
        else {
            second += 1;
        }
    }
// declaring friend

    friend void displayTime(const _Standard_Time&,
        const _Military_Time&);
};

class _Military_Time : public Time
{
    // methods for 24 Hour Clock
public:
    _Military_Time(int hr, int min, int sec)
    {
        hour = hr;
        minute = min;
        second = sec;
    }
    void addOneHour()
    {
        if (hour == 23) {
            hour = 0;
        }
        else {
            hour += 1;
        }
    }

    void addOneMinute()
    {
        if (minute == 59) {
            minute = 0;
            addOneHour();
        }
        else {
            minute += 1;
        }
    }

    void addOneSecond()
    {
        if (second == 59) {
            second = 0;
            addOneMinute();
        }
        else {
            second += 1;
        }
    }

    // Declare friend 
    friend void displayTime(const _Standard_Time&,
        const _Military_Time&);
};
The hr > 12 ? hr % 12 : hr expression is actually equivalent to hr % 13 (for hr < 24). What you actually want is hr > 12 ? hr - 12 : hr, or, more elegantly, (hr - 1) % 12 + 1.
Hello,

I figured out where I was going wrong. I just needed to step away from the project for a while so I could see the solution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 ERROR CODE

void addOneHour()   
    {
        if (hour == 11)  //FIX ME //if its currently 11:something 
        {

            hour = 0;  //Set hour to 00:something and flip AM PM ?

            am_pm = am_pm == AM ? PM : AM;
        }
        else {
            hour += 1;
        }
    }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 // NEW CODE
void addOneHour()
    {
        if (hour + 1 > 12)  // If hour + 1 is greater than 12
        {

            hour = 1;   // set hour to 1

            am_pm = am_pm == AM ? PM : AM;
        }
        else {   // else

            hour += 1; // increment hour 
        }
    }
hour + 1 > 12 is the same as saying hour > 11
Maybe step away again and consider that it's kind of dumb to have all these reimplementations of virtually identical algorithms. The only difference between 12- and 24-hour time is how they are displayed and read in. I would just store the seconds since midnight and a bool that determines if we should display as 12- or 24-hour time. Also, there's no reason to limit your "addXXX" functions to only be able to add 1 unit.

 
void addHour(int h = 1); // default adds 1 hour 

Why not use the existing date/time functions?
http://www.cplusplus.com/reference/ctime/

or for C++20:
https://en.cppreference.com/w/cpp/chrono
Why not use the existing date/time functions?

Obviously he's implementing his own little class for practice.
Driving home Dutch's point, if you record the seconds since midnight then:

1
2
3
4
5
6
7
8
unsigned hours = secondsSinceMidnight % 3600
if (am_pm) {
    hours %= 12;
    if (hours == 0) {
        hours = 12;
    }
}
// Hours now contains the hour value to display. 
Topic archived. No new replies allowed.