2016-01-21 66 views
0

我想從以下方法在C++中返回整數1:返回垃圾值,而不是0或C++

int check_for_chef(string str1,string str2,int M,int N) 
{ 
    if (N == -1) 
    { 
     cout << "I am returning 1." <<endl; 
     return 1; 
    } 
    else if (N > M) 
    { 
     cout << " I am returning 0." <<endl; 
     return 0; 
    } 
    else 
    { 
     if (str1[M] == str2[N]) 
     { 
      location[N] = M; 
      cout << "location is: "<<location[N]<<endl; 

      check_for_chef(str1,str2,M - 1, N - 1); 
     } 
     else 
     { 
      check_for_chef(str1,str2,M - 1, N); 
     } 
    } 

} 

但是,在返回我所得到的是:

Returned value is: 35668224 

整個代碼在這裏:

#include <iostream> 
#include <string> 

using namespace std; 

int location[4]; 

int check_for_chef(string str1,string str2,int M,int N) 
{ 
    if (N == -1) 
    { 
     cout << "I am returning 1." <<endl; 
     return 1; 
    } 
    else if (N > M) 
    { 
     cout << " I am returning 0." <<endl; 
     return 0; 
    } 
    else 
    { 
     if (str1[M] == str2[N]) 
     { 
      location[N] = M; 
      cout << "location is: "<<location[N]<<endl; 

      check_for_chef(str1,str2,M - 1, N - 1); 
     } 
     else 
     { 
      check_for_chef(str1,str2,M - 1, N); 
     } 
    } 

} 




int main() 
{ 
    int count = 0; 

    string original_string; 
    cin >> original_string; 

    string chef = "CHEF"; 

    int M = original_string.size(); 
    int N = 4; 

    while (1) 
    { 
     cout << "Returned value is: " << check_for_chef(original_string,chef,M - 1, N - 1); 
     cout << " i am in while."<<endl; 
     count++; 

     original_string.erase(location[3],1); 

     cout << "the original_string : " << original_string <<endl; 


     original_string.erase(location[2],1); 

     cout << "the original_string : " << original_string <<endl; 


     original_string.erase(location[1],1); 

     cout << "the original_string : " << original_string <<endl; 


     original_string.erase(location[0],1); 

     cout << "the original_string : " << original_string <<endl; 



     cout << "the original_string : " << original_string <<endl; 

     M = original_string.size(); 
     cout << "size is :" << M <<endl; 

     if (M < N) 
      break; 

    } 

    cout << count <<endl; 


} 

請幫我解決這個問題。

+0

我會建議使用http://cppcheck.sourceforge.net/來查找編譯器不會吐出或聲明爲警告的所有潛在錯誤... – 2016-01-21 12:06:52

回答

8

我看不出兩個return在我在下面的註釋行中添加的代碼

int check_for_chef(string str1,string str2,int M,int N) 
{ 
    if (N == -1) 
    { 
     cout << "I am returning 1." <<endl; 
     return 1; 
    } 
    else if (N > M) 
    { 
     cout << " I am returning 0." <<endl; 
     return 0; 
    } 
    else 
    { 
     if (str1[M] == str2[N]) 
     { 
      location[N] = M; 
      cout << "location is: "<<location[N]<<endl; 

      return check_for_chef(str1,str2,M - 1, N - 1); // here 1st RETURN 
     } 
     else 
     { 
      return check_for_chef(str1,str2,M - 1, N); // here 2nd RETURN 
     } 
    } 

} 
+0

我多傻!謝謝你的幫助。 – learner

+0

讓自己更好的編譯器,或在現有的編譯器中調整警告級別。你應該對此有警告。 –

3

您的代碼不會在else分支expicitly返回任何東西。 x84中的值通常通過EAX寄存器返回,所以如果不返回任何內容 - 它的行爲就像未初始化的變量。