Thread: Recognizing each digit of an integer

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    24

    Recognizing each digit of an integer

    Hi! I was wondering if there was a more effective way to recognize each digit of a given integer. Given an integer called name, this is what I got.

    Code:
        float name, a;
        name= 4734;
        int amountRead=0, maxRead=4617, f=1+log10(name),x,y,digits[f], b;
        a=name;
        
        for(int i=0; i< f; i++)
        {
                x=1+log10(a);
                y=a/pow(10, x-1);
                digits[i]=y;
                a-=y*pow(10, x-1);
                cout << "digits= " << digits[i] << endl;
        }
    Ohh and I was wondering if there any good sites to learn recursion. Thanks!!

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Isn't this in the FAQ?

    Lets take the cheat way
    Code:
    float somenum = 123452f;
    std::string digits;
    ostringstream oss;
    oss<<somenum;
    digits = oss.str();
    now you can use digits as an array of char with each containing a number. (Possible problem if the number actually has a decimal point in it)

    Note: Untested code, some mistake may appear. Finding them is left as an excersice for the user.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi! I was wondering if there was a more effective way to recognize each digit of a given integer. Given an integer called name
    Code:
    float name
    ??
    Possible problem if the number actually has a decimal point in it)
    ??
    Last edited by 7stud; 02-07-2006 at 11:20 PM.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Maf-o-matics

    With modulus and division:

    123456 % 100 = 56 Correct?
    56 / 10 = 5 Correct?

    113355 % 10000 = 3355 Correct?
    3355 / 1000 = 3 Correct?

    See how it works?
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. newbie programmer - needs help bad.
    By hortonheat in forum C Programming
    Replies: 17
    Last Post: 10-20-2004, 05:31 PM