Help

This is a discussion on Help within the C++ Programming forums, part of the General Programming Boards category; You should not have that while loop in function. You need to indent your code properly so that you can ...

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,395
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,395
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21