Can't figure out what's causing my program to bug up...(stacks)

So MOST of it is working out properly so far...until I get to this point. I'm supposed to check if brackets/braces are balanced. My code is below for the function definition:

assignment4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include "assignment4.h"

using namespace std;
typedef bool boolean;

namespace assignment4
{
	// Precondition: Each character of string p is '(', ')', '{' or '}'.
	// Postcondition: The method returns true if the characters form a
	//                   sequence of correctly balanced parentheses with each '('
	//                   matching a ')' and each '{' matching a '}'. Note that a
	//                   sequence such as ( { ) } is NOT balanced. On the other hand,
	//                   ( { } ) and { ( ) } are both balanced.
	boolean balanced(char* p)
	{
	    // Push an open brace/bracket onto the stack
	    // Once it reaches a closed brace, pop it off and THEN compare/check
	    // Stack needs to be empty at the end
	    // If it's even and if the stack's empty, it's balanced--return true
	    
	    // Declares a stack of chars for *p
	    stack<char> charstack;
	    
	    // Checks if the string is an even number first
	    if ((strlen(p) % 2) != 0)
	    {
	    	return false;
	    }
	    
	    // Continues to push or pop using a for loop
	    for (size_t i = 0; p[i] != '\0'; i++)
	    {
	        // If the char is a { or a (, push it onto charstack
	        if (p[i] == ('(' || '{'))
	        {
	            charstack.push(p[i]);
	        }
	       
	        // If the char is a } or a )
	        else if ((p[i] == (')' || '}')) && (!charstack.empty()))
	        {
	        	
	            if ((p[i] == ')') && (charstack.top() == '('))
	            {
	            	charstack.pop();
	            }
	            
	            else if ((p[i] == '}') && (charstack.top() == '{'))
	            {
	            	charstack.pop();
	            }
	
	            // Otherwise, if these two conditions aren't met, then the parentheses must not match; return false
	            else
	            {
	                return false;
	            }
	        }
// _____________________________________________________________________
else return false;
	    }
	  
	    // If the stack is empty AND the number of chars in the string were even, return true
	    if (charstack.empty())
	    {
	        return true;
	    }
	   
	    // If the stack is NOT empty or the number of chars in the string were odd, return false
	    return false;
	}
}


And the char array that's calling this function is:
char str4[20] = "{(({))}}";

It should be returning a false and NOT a true, since the brackets do not match up properly. Can anyone help me?

EDIT: I added a new line of code that seemed to fix the problem (See the underscored comment in the code), but now it's saying that THIS char array is unbalanced:
char str1[20] = "({({}{()})()})";
Last edited on
Nevermind...I still have a problem.

It looks like it's not pushing anything onto the stack. Anyone know how to work around this, or fix it? :/

EDIT: Dumb if-statement. I solved it.
Last edited on
EDIT: Dumb if-statement. I solved it.

Maybe you fix it in your post too, if other people stumble upon your code ;-)

Ciao, Imi.
Topic archived. No new replies allowed.