我想制作一个C++方法,将int分配给一个char数组。 并给出了int的一部分。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
我还以为是像this。但是,这并不为我工作,所以我写了:
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;
}
但是,
)您不能调用INT的大小这样。
)你不能只是像一个字符串说,我想为int i个元素,因为那么这种方法是没有用的
因此,没有人知道这可怎么解决呢?感谢您的帮助:)
'input.size()'其中'input'是'int' !!! – Nawaz
[在int数组中存储一个int的可能的重复?](http://stackoverflow.com/questions/1522994/store-an-int-in-a-char-array) – moooeeeep
你确定你的输入是int不是字符串? – qPCR4vir