+ 1
How to create a function to convert integers into arrays?
please advise
3 Answers
+ 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?
+ 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;
}
+ 1
ipang, I wanted to turn two integers into arrays, then add them together.
thank you all for your help