I want to make a C++ method that separates an int to a char array. And gives a part of the int back.
Example:
Input:
int input = 11012013;
cout << "day = " << SeperateInt(input,0,2); << endl;
cout << "month = " << SeperateInt(input,2,2); << endl;
cout << "year = " << SeperateInt(input,4,4); << endl;
output:
day = 11
month = 01
year = 2013
I thought it was something like this. But that doesn't work for me so I wrote:
int separateInt(int input, int from, int length)
{
//Make an array and loop so the int is in the array
char aray[input.size()+ 1];
for(int i = 0; i < input.size(); i ++)
aray[i] = input[i];
//Loop to get the right output
int output;
for(int j = 0; j < aray.size(); j++)
{
if(j >= from && j <= from+length)
output += aray[j];
}
return output;
}
But,
1) You can't call the size of the int this way.
2) You can't just like a string say i want element i of the int, because then this method is useless
How can this be solved?