+ 1

How to create a function to convert integers into arrays?

please advise

13th Feb 2018, 12:49 PM
Mich
Mich - avatar
3 Réponses
+ 5
Can you explain some more? I'm not sure I'm understanding your question here. What do you want to do, create an array and fill it with user input? or parse each digit of an integer into array?
13th Feb 2018, 2:30 PM
Ipang
+ 3
Try this: #include<algorithm> #include<iostream> #include<string> #include<array> using namespace std; array<int> To_Array(int num) { array<int,to_string(num).size()> a; // Array to hold the digits. int i=0; while(num!=0) {a[i]=num%10; i++; num/=10;} reverse(a.begin(),a.end()); // while got the digits in reverse. return a; // return back the array. } int main() { int n; cin>>n; // Read the number. array<int> arr(To_Array(n)); for(int i:arr) cout<<i<<endl; }
13th Feb 2018, 5:53 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
ipang, I wanted to turn two integers into arrays, then add them together. thank you all for your help
13th Feb 2018, 10:10 PM
Mich
Mich - avatar