C++ code output must be: Enter series of parentheses: Not Balance! What is the problem of this code? #include #include #include #include using namespace std; int main() { string x; stack> iS
C++ code output must be:
Enter series of parentheses: Not Balance!
What is the problem of this code?
#include <iostream>
#include <string>
#include <
#include <stack>
using namespace std;
int main()
{
string x;
stack<char, vector<char>> iStack;
bool status = true;
cout << "Enter series of parentheses: ";
getline(cin, x);
for (int i = 0; i < x.length(); i++)
if (x[i] == '(' || x[i] == '{' || x[i] == '[')
iStack.push(x[i]);
else
{
if (x[i] == ']')
{
if (iStack.empty() || iStack.top() != '[')
{
return false;
}
}
else if (x[i] == ')')
{
if (iStack.empty() || iStack.top() != '(')
{
return false;
}
}
else if (x[i] == '}')
{
if (iStack.empty() || iStack.top() != '{')
{
return false;
}
}
iStack.pop();
}
if (iStack.empty())
cout << "Balance!" << endl;
else
cout << "Not Balance!" << endl;
}

Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images









