我想從以下方法在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