我的cin之后我的代码崩溃了,我可能会得到关于我做错了什么的建议

问题描述 投票:0回答:1

我正在努力处理此代码,我似乎无法弄清楚。继承人的问题,编写一个使用单个堆栈检查包含大括号,括号和方括号的字符串是否正确地倾斜的程序?这是我到目前为止所写的,但是我似乎看不到它。没有错误,它可以编译,但是在我输入字符串后崩溃。我该如何解决?

#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;
c++ computer-science
1个回答
1
投票

您正在for循环中到达字符串末尾,请尝试:

for (unsigned int k = 0; k < s.length(); k++)

也考虑传递const std :: string&而不是复制参数。并使用命名空间std删除;

© www.soinside.com 2019 - 2024. All rights reserved.