Thread: Help

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should not have that while loop in function. You need to indent your code properly so that you can trace the logic correctly - you still have that final check for whether the stack is empty inside the for loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #32
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    you still have that final check for whether the stack is empty inside the for loop.

    didnt get you???

    should i remove the while??

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If we properly format:
    Code:
    int function()
    {
        for (int j = 0; j <10; j++)
        {
            if (name[j] =='(')
            {
                a.push(')');
            }
            if (name[j]=='[')
            {
                a.push(']');
            }
            if (name[j] == '{')
            {
                a.push('}');
            }
    
            if (name[j] ==')' || name[j]=='}' || name[j]==']' )
            {
                if (a.isEmpty()==0)
                {
                    return 0;
                }
    
                if (a.pop()!= name[j] )
                    return 0;
            }
    
            if (!a.isEmpty())
            {
                return 0;
            }
            else
                return 1;
    
        }
    }
    Now, it is obvious that this lies within the for loop:
    Code:
    if (!a.isEmpty())
    {
        return 0;
    }
    else
        return 1;
    Therefore, the for loop never makes it past the first iteration, i.e., it never considers any more than the first character. If that first character is an opening bracket, the stack is not empty, at the end of the loop, so it returns 0, even if the next character is a closing bracket.

    But the point behind this is to check, after everything has been processed, whether the stack is empty. If at that point the stack is empty, then all is well.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #34
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    i put that thing out ov for loop but its still not working ,, =((

  5. #35
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    Thanks Problem solved,,

  6. #36
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    specially to ANON, ROCKY MARRONE, LASERLIGHT

Popular pages Recent additions subscribe to a feed