I的值具有下面的代碼商店COUT
#include <iostream>
#include <math.h>
#include <string>
#include <sstream>
using namespace std;
int main() {
int value
cout << "Input your number: " << endl;
cin >> value;
string s = to_string(value);
const int count = s.length();
int position = count;
for (int i = 1; i < count + 1; i++)
{
int pwr = pow(10, position - 1);
cout << ((value/pwr) + position) % 10;
position--;
value = value % pwr;
}
相反COUT的,我怎樣才能使用for循環的((value/pwr) + position) % 10
值存儲到一個變量。非常感謝你的幫助。
[編輯] 我增加了一個陣列,而不是
int val[7];
int position = count;
for (int i = 1; i < count + 1; i++)
{
int pwr = pow(10, position - 1);
val[i-1] = ((value/pwr) + position) % 10;
position--;
value = value % pwr;
}
cout << "Encoded value is: ";
for (int i = 0; i < 8; i++)
{
cout << val[i];
}
它能夠輸出I想要的值,但有一個運行時故障#2 - 疊圍繞變量「VAL」被損壞。這是爲什麼?
剛剛創建矢量和推回你的結果 –
不能使用代碼,我們還沒有在 –
類學習@永亨邱。 http://en.cppreference.com/w/cpp/container/vector –