here is an example of the first suggestion i had:
Code:
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
string strNum;
cin >> strNum;
int i;
for (i = 0; i < strNum.length(); i++)
{
int temp = strNum.at(i) - '0';
cout << "digit is " << temp << ", digit*2=" << temp*2 << endl ;
}
return 0;
}
for each character in the input string, the character zero is subtracted from it, which will give its decimal value which is what you want. note that the input string is assumed to be an integer number.
edit: the above code illustration is to show that the individual characters (digits in this case) can be accessed and arithmetic can be performed on them.
there is no primitive datatype in c (or cpp, or java) that stores a number as an 'array' so that you can access the individual digits in that number. also i dont know of any other standard datatype that does this (sure, one may exist but if it did then you should have found it in your research).
are you trying to make 'really big' numbers and allow for algebraic operations on them? this seems to be a likely reason for wanting to do what your trying to do. this is different from wanting to do operations on the individual digits (independently). for example, you may want to do operations on the array of digits as a whole, such as '1','9' + '1' = '2','0' or you may want '1','9' + '1' = '1','10' or '1','0'. please narrow down which one your talking about. for the large number case, i thought java had this, so maybe look into that to see if there is a similar one for C++, or just look at its implementation details to see if you can make your own.
aside: regarding the looping suggestion, it was the alternative to the one i gave above (subtracting '0'). lets say we accept integer input ('int x') and the user types '567'.
loop 1: if you do 'x % 10' which is read 'x mod 10' the result is 7 (the least significant digit of x). now 'x / 10' gives x = 56.
loop 2: 'x % 10' = 6, the 2nd least significant digit of x. 'x / 10' gives x = 5.
loop 3: 'x % 10' = 5, the 3rd .... 'x / 10' gives x = 0;
loop 4: x == 0 so there are no more digits; stop.