正如標題所說,我必須找出給定數字中的位數。這裏是我的代碼,這使我的0的結果,當數必須高於10 編程在Visual Studio Pro的2012C++中的數字計數器
代碼:
#include <iostream>
using namespace std;
int main()
{
int g, count=0;
cout << "Enter the number" << endl;
cin >> g;
while (g > 0)
{
count = count +1;
g = g/10;
}
cout << count << endl;
system ("pause");
return 0;
}
提示:'std :: log10' – chris
謝謝克里斯,但我剛開始學習C++。你能解釋一下這是怎麼完成的嗎? – user2834298
什麼讓你相信循環條件應該是'(g> 0 && g <= 10)'?它應該更簡單! –