Wrapping a number around itself

This is a discussion on Wrapping a number around itself within the C++ Programming forums, part of the General Programming Boards category; Hi all, This is what im trying to do. Say for instance i have a variable of type int which ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    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.

    Thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Like
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Right.

    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, 12:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

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