Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to write a code to check the bracket pairs in an input string and print out either "Success" (for an input with matched pair) or the the 1-based index of the first unmatched closing bracket. However I'm getting the 1-based index for even matched bracket inputs. (for ex. for the input [ ] I'm getting an output of 1 instead of "Success"). I understand that there is a mistake in my logic. Could someone point out the mistake?

C++
#include <iostream>
#include <stack>
#include <string>

struct Bracket {
    Bracket(char type, int position):
        type(type),
        position(position)
    {}

    bool Matchc(char c) {
        if (type == '[' && c == ']')
            return true;
        if (type == '{' && c == '}')
            return true;
        if (type == '(' && c == ')')
            return true;
        return false;

    }

    char type;
    int position;
};


int main() {
    std::string text;
    getline(std::cin, text);
    int z;
    int len = text.size();

    std::stack <Bracket> opening_brackets_stack;
    for (int position = 0; position < len ; ++position) {
        char next = text[position];
        Bracket brackObj(next,0);

        if (next == '(' || next == '[' || next == '{') {
            opening_brackets_stack.push(brackObj);
        }

        if (next == ')' || next == ']' || next == '}') {

            if(brackObj.Matchc(next) == false || opening_brackets_stack.empty() == false)
            {
               z = position;
            }

            else
            {
                opening_brackets_stack.pop();
            }
        }
    }

    if (opening_brackets_stack.empty()==true)
    {
        std::cout << "Success";
    }

    else
    {
        std::cout << z;
    }
    return 0;
}


What I have tried:

I'm using a stack to push all the open brackets and pop them when a matching closing bracket is found.

For a non-matching input (for ex. ([{)] ) it should return an output of 4, and "Success" for a matching input ( for ex. ({[]}) ).
Posted
Updated 25-Sep-16 5:40am
Comments
[no name] 25-Sep-16 11:13am    
And when you stepped through your code, examining the variable values at each step of the way, what did you find?
Kishaan Jeeveswaran 25-Sep-16 11:17am    
I didn't initialise my integer "z". Is that what you asked about?
[no name] 25-Sep-16 11:23am    
The guy who writes the code should debug it. Have you used your debugger? That is what it is for.
Kishaan Jeeveswaran 25-Sep-16 11:33am    
I'm new to programming and I didn't know about debuggers earlier. Thank you for pointing it out! I'm using Codeblocks software. Will the debugger in this software serve the purpose?
[no name] 25-Sep-16 20:14pm    
Yes of course: http://wiki.codeblocks.org/index.php/Debugging_with_Code::Blocks

Um...The debugger would probably show you this very quickly!
Look at what you are doing:
C++
if(brackObj.Matchc(next) == false || opening_brackets_stack.empty() == false)
What is the type of brackObj when you try to compare it with a close bracket?
Hint: think about when you create each new instance of a Bracket. Are they all brackets? Are they all open brackets?
 
Share this answer
 
Comments
Kishaan Jeeveswaran 25-Sep-16 11:44am    
Well, I'm new to C++ and I code using 'codeblocks' software in Windows. The debugger in this software showed no errors when I compiled and ran it!
OriginalGriff 25-Sep-16 12:03pm    
That's not the debugger - that's the compiler!
Getting code through the compiler is the easy bit - that like saying that a sentence is valid English syntax, not that it is meaningful or says what you actually wanted.
For example, this is valid syntactically:
"The fishy building drove to the shops on a spaceship."
But meaningless for so many reasons :laugh:
Once it compiles the debugger is used to look at what it happening while the code is running, so you can spot "logic errors" and other mistakes in your code and / or design.
Look at your compiled code, and then at the menu bar - there is a "Debug" menu which allows you to set a breakpoint to the first line in your method.
When you run your code in the debugger it will stop when it reaches the breakpoint and let you take control, looking at variables, and executing lines one at a time (called single stepping) - it's worth getting used to this, you are going to be using it a lot (we all do!)
Kishaan Jeeveswaran 25-Sep-16 12:07pm    
Thanks a lot for taking your time to write out such a detailed reply. It's high time I should start using it. :)
OriginalGriff 25-Sep-16 12:15pm    
You're welcome!
Quote:
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900