Use the stack class in a program that reads a String, one character at a time, and determine whether the String contains balanced parentheses, that is , for each left parenthesis ( if there any ) there is exactly one matching right parenthesis later in the String .
so the issue is
if (xx) => correct
if ((xx)) =>correct
if(((x) => false .....
so i managed to do a big part of it, i just need to fix this last part:
bool stack::balanced(string st)
{
string strtmpl; // creating a string to hold the input in it
strtmpl = st; //creating a string template to store the string entered by the user
stack v; // overloading a stack
int xx; // creating a variable to hold string length in it
xx = strtmpl.length(); // filling variable xx with string length
for(int z=0;z<=xx;z++)
{
v.puch(strtmpl[z]); //pushing the string into a stack
}
int i=0;
int n =0;
//cout<<i;
//cout<<(char(v.StackArray[v.stacktop-1]));
while(char(v.StackArray[i]) == '(')
{
if ((char(v.StackArray[v.stacktop-1])) == ')' && strtmpl[i] == '(')
{
if (char(v.StackArray[v.stacktop-1]) == ')')
{
n++;
}
v.pop();
//cout<<char(v.StackArray[v.stacktop-1]);
//i++;
//xx--;
//return 1;
}
//else
//{
//cout<<"bad!";
// n++;
//xx--;
//}
i++;
}
if (n++ != i++)
cout<<"bad";
else if ((char(v.StackArray[v.stacktop-1])) != ')')
cout<<"good";
return 0;
}