int を char 配列に分離する C++ メソッドを作成したいと考えています。そして、int の一部を返します。
例:
入力:
int input = 11012013;
cout << "day = " << SeperateInt(input,0,2); << endl;
cout << "month = " << SeperateInt(input,2,2); << endl;
cout << "year = " << SeperateInt(input,4,4); << endl;
出力:
day = 11
month = 01
year = 2013
こんなものかと思った。しかし、それは私にはうまくいかないので、私は書きました:
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;
}
しかし、
1 ) この方法で int のサイズを呼び出すことはできません。
2)文字列のように、intの要素iが必要だと言うことはできません。その場合、このメソッドは役に立たないためです
これはどのように解決できますか?