1. ## switching a bool

Can I switch a bool object from another function. I am writing a function that advances a clock a certain number of minutes.

If the hours end up being more than 12, I need to switch my bool variable that determines whether it is am or pm. So if it's 4am and I add 9 hours (540 minutes), I need to switch the bool from true to false.

Can I do that?

Brian

2. Code:
```bool var = true;
var = false;
var = true;
// etc...```
This what you mean?

3. Right. My bool tells me that if its true, it's AM, if it's false, it's PM. So if I advance my clock past AM or PM, I need to change from AM to PM, or true to false or vice versa.

I know I can do this:

Code:
```if (_hours > 12)
{
_hours = _hours + (_hours % 12);
if (_morn == true)
_morn = false;
else
_morn = true;
}```
But I was wondering if there is a way to just switch the bool.

Brian

4. Code:
```bool var;
var = !var;```
That will switch a boolean variable from true to false or false to true.

5. EDIT:
on 2nd thought, I'm being too complicated, joshdick's method was more obvious.

6. Thanks all!

7. Though I cannot see all of your program, that piece of code you provided looks a bit suspicious. I may be misinterpreting what your variables are, but this is what I see.

If '_hours' is greater than 12, it is the afternoon, so you would want to set '_morn' to false regardless of what it previously was.

Also, the line '_hours = _hours + (_hours % 12);' seems a bit suspicious. For example, if it is 2 PM, _hours == 14 initially. 14 % 12 = 2, so _hours is set to 14 + 2 = 16. Is this what you are going for?

Anyways, hope this is somewhat useful. Cheers

8. Zach -- he's talking about advancing the clock by x hours -- so if he advances more than 12, it needs to switch AM/PM.

HOWEVER, this way is flawed, too. What if you advance by more than 24 hours in a go?

I recommend instead that you simply make a class to do this, and internally simply store the time as an integer number of minutes, where 0 represents 12:00 AM, and 1439 represents 11:59 PM. It's quite easy to get each of the parameters (hours, minutes, AM/PM) from this single number, as follows:

int hours = (m_time/60) % 12;
if (hours == 0) hours = 12;
int minutes = m_time%60;
bool AM = m_time/60 < 12;
bool PM = m_time/60 > 11;

Incrementing time is also easy:

m_time = (m_time + increment) % 1440;