in function stars code is returning garbage value if i input date between 20 Dec - 19 FEB (it should return 0) i want it to return either 0,1,2,3,4 satisfying the condition...

Please help me with this problem!!

#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

int stars(int date, int month);
int check(const char* arr);

int main()
{
    string arr;
    int cont=1;
    int chck,date,month;
    string segment[]= {"WOOD","FIRE", "EARTH","GOLD","WATER"};
    string animal[]= {"DOG","CAT","RAT","TIGER","MONKEY"};


    while (1)
    {
        cout << "Enter your Date of Birth OR 0 to exit\n(Format: DD/MM/YYYY e.g: 11/09/1984): ";
        getline(cin,arr);
        date=((arr[0]-'0')*10)+(arr[1]-'0');
        month=((arr[3]-'0')*10)+(arr[4]-'0');
        if (arr == "0")
        {
            cout << "Thankyou for using... :)";
            exit(1);
        }
        else
        {

            if (arr.length() != 10 )
            {
                cout << "\nSyntax Error! Please follow the syntax\n\n";

                continue;
            }


            chck = check(arr.c_str());
            if(chck != 0)    // Checking the errors in syntax
            {
                if (chck == 1)
                {
                    cout << "\nError in Date. Please try again and follow the syntax\n";
                }
                if (chck == 2)
                {
                    cout << "\nError in Month. Please try again and follow the syntax\n";
                }


                if (chck == 4)
                {
                    cout << "\nError in Syntax. Please try again and follow the syntax\n";
                }
                cont=1;
            }

        }
        int temp=stars(date,month);
        cout << temp << "You are" << segment[(stars(date,month))] <<  "/" << animal[(stars(date,month))] << endl;

        //cout << date << endl << month ;
    }

    return 0;
}

int stars(int date, int month)
{
    if( ((month == 12) && (date >=20)) || (month == 11 )|| ((month == 2) && (date <= 19) ))
    {
        return 0; //WOOD
    }

    if ( (month  >= 2) && (month <= 4))
    {
        if(((month == 2) && (date >= 20)) ||(month ==3) || ((month == 4) && (date <= 19)))
        {
            return 1; // FIRE
        }

    }

    if (((month  >= 4) && (month <= 7)) || (month == 5) || (month == 6))
    {
        if (((month == 4) && (date >= 20)) || ((month == 7) && (date <= 19)))
        {
            return 2; // EARTH
        }

    }

    if ( month  >= 7 && month <= 10 || month == 8 || month ==9)
    {
        if(month == 7 && date >= 20 || month == 10 && date <= 19)
        {
            return 3; // GOLD
        }

    }

    if ( ((month  >= 10) && (month <= 12)) || (month == 11))
    {
        if (((month == 10) && (date >= 20)) || ((month == 10) && (date <= 12)))
        {
            return 4; // Water
        }

    }
}

int check(const char* arr)
{
    if (arr[0] < '0' || arr[0] > '3' || arr[1] < '0' || arr[1] > '9' || ( arr[0] == '3' && (arr[1] != '0' || arr[1] != '1') ))
    {
        return 1;// Date problem
    }

    if ( arr[3] < '0' || arr[3] > '1' || arr[4] < '0' || arr[4] > '9' || (arr[3] == '1' && (arr[4] <'0' || arr[4] > '2')))
    {
        return 2; // month problem
    }

    if( arr[2] != '/' && arr[5] != '/')
    {
        return 4;
    }

    return 0;
}

If it returns a "garbage" value, there's a very high probability that it isn't returning anything at all, so check that function to make sure everything returns a value. Stick a debugging statement after line 112...

cout << "Uh-oh.  Should never get here.\n";

If that line prints, that means you need to fix your function because you have a bunch of if-statements with return statements, but something slipped through the cracks. You really need to have some error recovery at the bottom to make sure the program stops, recovers, or alerts you when something unexpected like that happens. Every legitimate parameter to a function should return something. The illegitimate parameters really should too.

every thing is fine its returning the garbage value when it should return 0;

else it is returning 1,2,3,4 correctly

>> every thing is fine its returning the garbage value when it should return 0;

Then it isn't fine. So is the line I suggested printing? What's value of the garbage value?

This article has been dead for over six months. Start a new discussion instead.