find the digits

This is a discussion on find the digits within the C++ Programming forums, part of the General Programming Boards category; I want to transform a number into a vector of it's digits. For example, 123 will be 1 2 3. ...

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452

    find the digits

    I want to transform a number into a vector of it's digits. For example, 123 will be 1 2 3.
    I came up with this code:

    Code:
    void transform(const uint & x , deque<uint> & output)
    {
      if ( x != 0 )
        {
          uint x_stripped = (x/10)*10;
          uint digit = x - x_stripped;
          output.push_front(digit);
          transform(x_stripped / 10, output);
        }
    }
    My question is, is there better way to do this? taking x and divide then multiply by 10 is a bit irksome for me.
    Last edited by nimitzhunter; 01-05-2011 at 01:35 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    You could try:
    Code:
    void transform(const uint& x , deque<uint>& output)
    {
        do
        {
            output.push_front(x % 10);
            x /= 10;
        }
        while (x != 0);
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    Quote Originally Posted by laserlight View Post
    You could try:
    Code:
    void transform(const uint& x , deque<uint>& output)
    {
        do
        {
            output.push_front(x % 10);
            x /= 10;
        }
        while (x != 0);
    }
    laserlight, x/=10 wouldn't work because x is const. Maybe I could create a temp variable instead.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    Quote Originally Posted by nimitzhunter
    laserlight, x/=10 wouldn't work because x is const. Maybe I could create a temp variable instead.
    Oh yeah, I did not notice that. Just change to use pass by value since passing by const reference is not likely to gain you anything here.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    Quote Originally Posted by laserlight View Post
    Oh yeah, I did not notice that. Just change to use pass by value since passing by const reference is not likely to gain you anything here.
    Thanks alot for your help, laserlight. It works perfectly :-D.
    "All that we see or seem
    Is but a dream within a dream." - Poe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Counting letters and digits
    By FeNCinGeR in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2006, 11:39 AM
  3. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 07:28 AM
  4. Odd/Even Digits in a Number-Help!
    By ProgrammingDlux in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2002, 09:39 PM
  5. Max Digits
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-09-2002, 05:28 AM

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