switching a bool

This is a discussion on switching a bool within the C++ Programming forums, part of the General Programming Boards category; Can I switch a bool object from another function. I am writing a function that advances a clock a certain ...

  1. #1
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    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
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Code:
    bool var = true;
    var = false;
    var = true;
    // etc...
    This what you mean?
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66
    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
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Code:
    bool var;
    var = !var;
    That will switch a boolean variable from true to false or false to true.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    EDIT:
    on 2nd thought, I'm being too complicated, joshdick's method was more obvious.

  6. #6
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66
    Thanks all!
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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;
    Last edited by Cat; 11-12-2003 at 09:15 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing params between managed c++ and unmanaged c++
    By cechen in forum C++ Programming
    Replies: 11
    Last Post: 02-03-2009, 07:46 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  5. Need Help With an RPG Style Game
    By JayDog in forum Game Programming
    Replies: 6
    Last Post: 03-30-2003, 07:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21