Thread: Help me with this error plzz

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    82

    Help me with this error plzz

    every thing is working fine except when I input date between Dec 20 -Feb 19

    when i input date between this it returns the date from function stars.. line 71

    HELP ME PLZZZ!!!


    Code:
    #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;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Fix the warnings perhaps?
    Code:
    $ g++ -Wall foo.cpp
    foo.cpp: In function ‘int stars(int, int)’:
    foo.cpp:80: warning: suggest parentheses around ‘&&’ within ‘||’
    foo.cpp:80: warning: suggest parentheses around ‘&&’ within ‘||’
    foo.cpp:87: warning: suggest parentheses around ‘&&’ within ‘||’
    foo.cpp:89: warning: suggest parentheses around ‘&&’ within ‘||’
    foo.cpp:96: warning: suggest parentheses around ‘&&’ within ‘||’
    foo.cpp:98: warning: suggest parentheses around ‘&&’ within ‘||’
    foo.cpp:105: warning: suggest parentheses around ‘&&’ within ‘||’
    foo.cpp:107: warning: suggest parentheses around ‘&&’ within ‘||’
    foo.cpp:114: warning: control reaches end of non-void function
    Chances are, your expressions of && and || without ( ) do not mean what you think they mean.

    Also, if the code reaches the end of the function, you get a garbage value (which you then use as an index to an array)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    As you have been informed, things get tricky when you start string together lots of logical evaluations,
    so it is better to compartmentalise them into their own little groups so you are clearly showing what
    each evaluation is relative to the next.

    Code:
    if ( month  >= 4 && month <= 7 || month == 5 || month == 6) 
    
    //it is better to write something like:
    
    if ( ((month  >= 4) && (month <= 7)) || (month == 5 || month == 6))
    Also consider using descriptive lists for things that are fixed, like the months of the year, this way your code becomes much more
    readable, and makes more sense instead of having 'magic numbers' floating all over the place:

    Code:
    enum year_month
    {
    	JAN = 1,
    	FEB,
    	MAR,
    	APR,
    	MAY,
    	//etc...
    };
    
    enum err_val
    {
    	RET_OK,
    	ERR_DATE,
    	ERR_MONTH,
    	ERR_SYN
    };
    
    //equally do ones for your 'wood, fire gold and animals etc etc
    
    //more code....
    
    //and using this your if statement described above would look more like:
    
    if ( month  >= APR && month <= JULY || month == MAY || month == JUN) 
    
    //which is lots better.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    82

    can't solve the prob :(

    i can't solve the problem.. i dnt understand why its returning garbage value

    i tried to remove the warnings


    Code:
    #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;
    }

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    mate you are not evolving,

    Get the habit of giving more info in your pleas, like what errors do you get now? What do you think is hapenning, etc, etc, etc, etc, etc

    You seem to have amended some of the if statements but then lost interest with the other similar ones and didnt bother?

    It is not neccesary to encapsulate every evaluation in its own set of ' ( )' as you have done in a couple of instances. the idea is to work it out and break the logic down into sensible groupings. Having said this it is unlikely to be the cause of your overall problem, its just better and more well defined logic doing it like that, you are more likely to get the evaluation result you expect.

    Looking at it again you only seem to have missed out on tidying up one of the if statements, around line 95, try fixing that up first, then come back with a proper description of what is going wrong

    And for my money while(1) sucks in this context, name a variable, a boolean handily supplied by c++ for your enjoyment.

    this is not very attractive:
    Code:
    cout << temp << "You are" << segment[(stars(date,month))] <<  "/" << animal[(stars(date,month))] << endl;
    PS you remember in Salem's reply 'fix the warnings' ?? Did you notice the one that says it reaches the end of a non void function?

    Well if your program flow reached the end of the function i.e your if statements failed to catch anything, then what would be returned? And a certain array seems to depend on a usable return value from stars()....
    Last edited by rogster001; 02-04-2012 at 07:54 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. check it asap plzz...........
    By Hira Khawer in forum C++ Programming
    Replies: 2
    Last Post: 04-19-2010, 07:47 AM
  2. I need help plzz :(
    By Kawaii JoJo in forum C++ Programming
    Replies: 11
    Last Post: 11-06-2009, 08:15 AM
  3. help me out plzz!!
    By salmanriaz in forum Game Programming
    Replies: 11
    Last Post: 03-20-2009, 09:56 AM
  4. plzz help
    By aj11 in forum C++ Programming
    Replies: 10
    Last Post: 11-25-2007, 01:19 PM
  5. need help , plzz
    By kfatima in forum C Programming
    Replies: 1
    Last Post: 10-11-2002, 09:19 PM