简体   繁体   中英

Returns garbage value instead of 0 or 1 in c++

I am trying to return integer from the following method in 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);
        }
    }

}

But, what I am getting while returning is :

Returned value is: 35668224

Whole code is here:

#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;


}

Please help me to solve this problem.

I don't see two more return in the code

I have added in the commented lines below:

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
        }
    }

}

Your code does not return anything expicitly in the else branch. Values in x84 usually are returned via EAX register, so if you do not return anything - it behaves like an uninitialized variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM