I was doing parenthesis checker using the stack. One of the if else statement containing a break; statement is causing segment fault.
I tried removing the break program runs fine but prints the wrong answer as that break is needed to print correct output. What is the cause of such a segment fault? the break doesn't access any memory unit.right?
#include <iostream>
#include<stack>
using namespace std;
int main() {
//code
int n;
char c,comp;
cin>>n;
while(n--)
{
stack<char>s;
while(cin>>c)
{
if(c=='(' || c=='{'|| c=='[')
s.push(c);
else
{
comp=s.top();
if(c==')' && comp=='(')
s.pop();
else if(c==')' && comp!='(')
{
cout<<"not balanced"<<endl;
break; //this one, if i remove this no SIGSEGV
}
if(c=='}' && comp=='{')
s.pop();
else if(c=='}' && comp!='{')
{
cout<<"not balanced"<<endl;
break;
}
if(c==']' && comp=='[')
s.pop();
else if(c==']' && comp!='[')
{
cout<<"not balanced"<<endl;
break;
}
}
}
if(s.empty())
cout<<"balanced"<<endl;
}
return 0;
}