我想製作一個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