# Thread: Storing a number in an array

1. ## Storing a number in an array

Is there a fast way to store a number in an array like:

Input: 234586349

becomes

Code:
`int array[] = {2,3,4,5,8,6,3,4,9}`

Right now my method is very tricky :/

To get the 6, I have to do:

Code:
```int a= 234586349 / static_cast<int>(pow(10.0,3));                         // gives 234586
int b = (234586349 / static_cast<int>(pow(10.0,4))) * 10;              // gives 234580

int x = a-b;       // gives 6```
Then somehow I have to use a FOR function to get all the digits... is there a better way?
(Preferably one that doesn't involve floating numbers and static_casts I always get into trouble around them >_<)

Thanks!

2. Well, input is originally a string anyway, so I'd declare the input variable as a string. Then, if your array has to be an array of integers, you can convert your string digit by digit (just make sure you actually get the number you want rather than the ASCII code for the number character) for input into your array.

3. Code:
```#include <iostream>
#include <sstream>
using namespace std;

inline int cToInt(const string& s){
istringstream i(s);
int x;
i >> x
return x;
}

int main(){
string  x = "123456789";
int array[9];

for(int i = 0; i < 9; i++){
array[i] = cToInt(x.substr(i, 1));
cout << array[i] << " ";
}
}```

4. Thanks aisthesis, and gamer4life687 for the code!
I'm new to a lot of these functions but I'll research them

Popular pages Recent additions