我想从以下方法在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;
}
请帮我解决这个问题。
我会建议使用http://cppcheck.sourceforge.net/来查找编译器不会吐出或声明为警告的所有潜在错误... – 2016-01-21 12:06:52