Thread: Wrapping a number around itself

  1. #1
    Registered User
    Join Date
    Oct 2004

    Wrapping a number around itself

    Hi all,

    This is what im trying to do. Say for instance i have a variable of type int which keeps track of the time. The variable takes numbers 1 to 12. Every time the int is incremented it represents another hour. Howerver when the number gets to 12 instead of incrementing to 13 it should go back to itself.

    I know this can be done using if statements to reset it. But in another programming language there was a built in command that dealt with it (i believe it was called mod). Im guessing there is a C++ alternative to this command but cannot seem to find one.


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    h = ( h + 1 ) % 12;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001

    In C/C++, the modulus operator is represented by the percent sign.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM