Thread: Help manipulating int

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    10

    Help manipulating int

    What is the most efficient way to get the last 2 digits of an integer into a seperate variable? What about the 3rd and 4th last?
    Ex.
    123456
    int a should == 12
    int b should == 34
    int c should == 56

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You can use the modulus operator to get the remainder of a division operation. If you divide a number by 100, the remainder is going to be the last two digits of that number.

    Getting the third and fourth is a very similar process. Get the last four digits, then divide by 100 (integer division just drops the remainder in C++) to get the first two.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    Great! Thanks.

    In case you were wondering, this isn't homework. I'm still in high school. This is for my own personal health.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM