在這段代碼,我想知道是什麼之後,如果塊執行的代碼中的其他執行和代碼之間的區別?執行的其他塊碼後,如果塊
#include <iostream>
using namespace std;
int digits(int number, int value, int times){
int counter = times;
counter++;
int result = number/value;
value=value*10;
if (result != 0){
digits(number,value,counter);
}
return counter;
}
int main() {
int number,no_of_digit;
cout << "Enter the number : ";
cin >> number;
no_of_digit = digits(number, 10, 0);
cout << "No of digits are : " << no_of_digit << "\n";
return 0;
}
此代碼無法正常工作。它給出值1. 但是,如果我把return counter;
在其他塊它的作品。爲什麼這樣?
使用正確的工具來解決這些問題是你的調試器。您應該先詢問Stack Overflow,然後逐行執行代碼。如需更多幫助,請閱讀[如何調試小程序(由Eric Lippert撰寫)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,你應該[編輯]你的問題,以包含一個[Minimal,Complete,and Verifiable](http://stackoverflow.com/help/mcve)例子來重現你的問題,以及你在調試器中所做的觀察。 –