C++ Program to check for balanced parentheses in an expression using stack.

Write your question here.
Hi guys, as the title states, I'm trying to create a program that takes string as an input, searches through it, and checks whether or not if the parentheses are "balanced" e.g. "({})" or "[]()".

Once the program is run, strings two and three are displayed as "Not balanced!" which is what I was expecting. However, string one is not displaying any result at all. If I were to comment out isBalanced for two and three, nothing would appear.

Can anyone please explain the reason behind this? I'm aware there are solutions for this particular problem but I'm really more interested in the mistakes I made and how I can improve my code.

I'm new to programming and with the C++ language, so I apologize if my question seems a bit silly. Thanks :)
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
  //Here is my code:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stack>

void isBalanced(std::string s)
{
	int x = s.length();
	std::stack <char> mstack;

	for (int i = 0; i < x - 1; i++)
	{
		if (s[i] == '(' || s[i] == '[' || s[i] == '{')
		{
			mstack.push(s[i]);
		}

		else
		{
			if (mstack.top() != s[i] || mstack.empty()) return;

			else mstack.pop();
			
		}
	}

	(mstack.empty()) ? std::cout << "Balanced!" : std::cout << "Not balanced!";
}

int main()
{
	std::string one = "(){}()";
	std::string two = "({)";
	std::string three = "{{(]";

	isBalanced(one);
	isBalanced(two);
	isBalanced(three);




	
    return 0;
}
Can anyone please explain the reason behind this?
Line 21: The return will end the function without displaying anything.

It cannot be correct siince you are not checking for the the closing parentheses.
for (int i = 0; i < x - 1; i++)
This will skip the last char in the string
 
if (mstack.top() != s[i])

It will always be true if you have anything but an opening brace.
It also would be better of isBalanced would return a bool and you do the output in main.
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
#include <iostream>
#include <string>
#include <stack>


bool isEndBracket( char saved, char check )             // <==== checks if this is the correct end bracket
{
   return ( ( saved == '(' && check == ')' ) ||
            ( saved == '[' && check == ']' ) ||
            ( saved == '{' && check == '}' ) );
}



void isBalanced(std::string s)
{
	int x = s.length();
	std::stack <char> mstack;

	for (int i = 0; i < x ; i++)             // <==== x, not x-1
	{
		if (s[i] == '(' || s[i] == '[' || s[i] == '{')
		{
			mstack.push(s[i]);
		}
		else if ( !mstack.empty() && isEndBracket( mstack.top(), s[i] ) )           // <==== check for empty first
		{
                        mstack.pop();
		}
	}

	(mstack.empty()) ? std::cout << "Balanced!\n" : std::cout << "Not balanced!\n";
}

int main()
{
	std::string one = "(){}()";
	std::string two = "({)";
	std::string three = "{{(]";

	isBalanced(one);
	isBalanced(two);
	isBalanced(three);

    return 0;
}
Ah , I see, thanks for pointing out my mistakes guys! :)
Topic archived. No new replies allowed.