Recognizing each digit of an integer

• 02-07-2006
sybariticak47
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!!
• 02-07-2006
Thantos
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.
• 02-07-2006
7stud
Quote:

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`
??
Quote:

Possible problem if the number actually has a decimal point in it)
??
• 02-08-2006
SlyMaelstrom
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?