My code isn't working properly, can't find the error.

This code determines if the following brackets are balanced or not.

But for some reason in the third case, is marking it as unbalance and it supposed to be balanced.

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
77
78
#include <iostream>
#include "ArrayStack.h"
#include <string>
using namespace std;

// Function prototype
bool checkallBraces(char []);

int main()
{
	char * s;
	s = new char;
	string input;

	while (!(input == "q"))
	{
		cout << "This program checks if an expression is balanced.\nEnter expression: ";
		cin >> s;

		if (checkallBraces(s))
			cout << "Expression is balanced!\n";
		else
			cout << "Expression isn't balanced!\n";
	}
	system("pause");
	return 0;
}


bool checkallBraces(char s[])
{
	char ch = '\0';
	char stackItem = '\0';
	bool balancedSoFar = true;
	ArrayStack<char>* aStack = new ArrayStack<char>;

	// iterate through each character in the string while checking continue

	for (int i = 0; i < sizeof(s) && balancedSoFar; i++)
	{
		// set ch to current pos in string
		ch = s[i];

		// Ignore all characters but (, {, [, ], } and )
		if ((ch == ')') || (ch == '(') || (ch == '}') || (ch == '{') || (ch == ']') || (ch == '['))
		{
			// process open brace
			if (ch == '{' || ch == '(' || ch == '[')
			{ // el error estaba aqui
				aStack->push(ch);
			}

			// process closed brace
			else if (!aStack->isEmpty()) 
			{
				// peek into stack to find opening brace
				stackItem = aStack->peek();

				// try to match open and close delimiters of the same kind
				if (((ch == ')') && (stackItem == '(')) || ((ch == '}') && (stackItem == '{')) || ((ch == ']') && (stackItem == '[')))
					aStack->pop(); // Pop a matching open brace if found.
				else // No matching open brace, Open and close parenthesis are not of the same type
					balancedSoFar = false;
			}

			// Unmatched open and close parenthesis
			else
				balancedSoFar = false;
		}
	}

	 //return true if stack is empty and balanced
	if (aStack->isEmpty())
		return (true && balancedSoFar);

	// else it isn't blanced
	else return false;


OUTPUT

This program checks if an expression is balanced.
Enter expression:{}
Expression is balanced!

// OK! NO PROBLEM !

This program checks if an expression is balanced.
Enter expression:{a{bc}
Expression isn't balanced!

// OK! NO PROBLEM!

This program checks if an expression is balanced.
Enter expression:{a{b}c}
Expression isn't balanced!

CAN SOMEONE EXPLAIN ME WHY IT ISN'T BALANCE ?
IT SUPPOSED TO BE BALANCED!
Last edited on
don't yell.


for (int i = 0; i < sizeof(s) && balancedSoFar; i++)
`s' is a pointer,
sizeof is evaluated at compile time
so you may understand why you are not iterating the whole string.
you may use strlen(), check if s[i] is the terminator '\0' character, or use std::string that knows its size.


> ArrayStack<char>* aStack = new ArrayStack<char>;
1
2
ArrayStack<char> aStack; //construct an object
a.pop(); //use it as 

http://www.cplusplus.com/forum/general/138037/#msg731921


1
2
	char * s;
	s = new char;
you just reserved enough space for 1 character. You cannot even use cin>>s because it needs to write the terminator character.

You better use std::string
1
2
3
4
5
6
7
8
std::string input;
while( std::getline(std::cin, input) and input not_eq "q" ){ //getline() reads a whole line, including spaces
   //...
}

bool checkallBraces(const std::string &s){
   for (int i = 0; i < s.length() && balancedSoFar; i++)
}
Last edited on
Your logic is correct. The third case passed when I ran your code.
http://cpp.sh/5tb2
I removed #include "ArrayStack.h" and used #include <vector> instead. Maybe something is wrong with ArrayStack?
Thanks for taking the time to help! The problem was sizeof(s), I change it to strlen() and it worked.
Topic archived. No new replies allowed.