2016-01-21 59 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寄存器返回,所以如果不返回任何内容 - 它的行为就像未初始化的变量。