I am struggling with this code, i cant seem to figure this out. Heres the question, Write a program that usues a single stack to check wether a string containing braces, parenthesis, and brackets is properly delimted? this is what i wrote so far but i cant seem to see it. There are no errors and it does compile but it crashes after I enter my string. How do I fix it?
#include <iostream>
#include <string>
#include <stack>
using namespace std;
bool isbalanced(string);
int main()
{
string s;
cout<< "This program checks to see if the delimiters are properly balanced" << endl;
cout << "Enter a string with some paranthesis" << endl;
getline(cin, s);
if (isbalanced(s))
cout << "The string has balanced paranthesis" << endl;
else
cout << "String does not have balanced parathesis" << endl;
return 0;
}
bool isbalanced(string s)
{
stack<char> stack;
char ex;
for (unsigned int k = 0; k = s.length(); k++)
{
switch (s[k])
{
case '(': stack.push(')');
break;
case'{': stack.push('}');
break;
case '[': stack.push(']');
break;
case ')':
case '}':
case ']':
ex = stack.top();
stack.pop();
if (ex != s[k])
{
return false;
}
break;
deafult:break;
}
}
if (stack.empty())
return true;
else
return false;