Thread: How do I seperate integers?

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    How do I seperate integers?

    Hi,

    I'm doing this little program at the moment where I have to imput a 5-digit integer, seperate the number into its individual digits and prints the digits seperated from one another by three spaces each.
    It gives me a hint that I should use the division and modulus operators.

    Iv'e started like this, but really am clueless how to tackle this, can anyone give me a few tips and/or pointers

    thanks for any help

    Code:
    int main(int argc, char* argv[])
    {
       int number;
    
       cout << "Enter a five digit integer: ";
       cin >> number;
    
       number = number / 10;
       cout << number;
    
       number = number / 100;
       cout << number;
    
       number = number / 1000;
       cout << number;
    
       number = number / 10000;
       cout << number;
    
       getch();
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    167
    Here's how I would do it

    If 51236 is entered,

    Code:
     
    51236 / 10000 = 5 rem 1236 
     1236 /  1000 = 1 rem 236 
      236 /   100 = 2 rem 36 
       36 /    10 = 3 rem 6 
        6 /     1 = 6
    silk.odyssey

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    yes, but the input is not pre-determined. and i have never comes across "rem" before yet.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    167
    Guti14,

    rem just meant remainder. I know the input is not predetermined, I was just suggesting a way you could write the program.

    51236 / 10000 = 5 rem 1236

    51236 divided by 10000 is 5 with 1236 remaining. So we know that the first number is 5.

    1236 / 1000 = 1 rem 236

    1236 divided by 1000 gives us 1 with 236 remaining. So we know 1 is the second number.

    236 / 100 = 2 rem 36
    36 / 10 = 3 rem 6
    6 / 1 = 6

    Same idea for the other numbers. It's a simple algorithm and it should work for any 5 digit number.
    silk.odyssey

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    For an arbitrary number of digits, you need only compute the number of decimal digits to use in a loop. The number of digits will be floor(log10(n)) + 1.
    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.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    You don't need to know the number of digits to print it out backwards, this is probably giving too much help but.
    Code:
        std::cout << n % 10;
        while(n /= 10) std::cout << "   " << (n % 10);
        std::cout << std::endl;

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    ^ True, but to print it out forward you probably should, but it could be done otherwise:

    Code:
    unsigned reverse(unsigned x)
    {
      unsigned t = 0;
    
      do
      {
        t *= 10;
        t += x % 10;
        x /= 10;
      } while(x);
    
      return t;
    }
    And then grib's code would work.
    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.

  9. #9
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    silk.odyssey,

    Actually, those aren't reamainders. 51236 / 10000 = 5.1236
    Note the decimal. So they aren't reamainders, but decimals. Use a calculator if you don't believe me.

    -SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Sir Crono, we aren't using a calculator, we're using C++, the result of int/int is another int (truncated (a/b)*b+a%b==a).

    Zach, I think your right and the op wanted it forwards, hmmm. Must come up with goofier way do to this.

    Code:
    void f(unsigned n) {if(n) {f(n/10); std::cout << (n % 10) << "   ";}}

  11. #11
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Yeah, I know. I guess I was just thinking math - math. Int, integer, whole number. Float, float, decimal. Char, character, letter...

    -SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by grib

    Code:
    void f(unsigned n) {if(n) {f(n/10); std::cout << (n % 10) << "   ";}}
    Nice.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  3. Integers into array.
    By livestrng in forum C Programming
    Replies: 10
    Last Post: 10-29-2008, 11:35 PM
  4. Pass by reference
    By jrice528 in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2007, 01:02 PM
  5. Replies: 6
    Last Post: 08-04-2003, 10:57 AM