【问题标题】:"Debug Assertion Failed!" issue in a Caesar Cipher program“调试断言失败!”凯撒密码程序中的问题
【发布时间】:2017-05-11 13:42:46
【问题描述】:

你好!

目前在 C++ 中创建一个程序,其目的是用 Caesar Cipher 加密一个短语,并且在尝试了几次调试后无法弄清楚问题出在 em>Microsoft Visual Studio 2017 社区

问题

reference operator[](const size_type _Off)
    {   // subscript mutable sequence
    _IDL_VERIFY(_Off <= this->_Mysize(), "string subscript out of range"); //(Here, a breakpoint is triggered)
    return (this->_Myptr()[_Off]);
    }

没有错误或警告显示在 错误列表 但在运行时我的程序“跳转”到一个名为“xstring”的库并在这个函数内停止(参见代码)。

调试输出

    Debug Assertion Failed!

Program: C:\Windows\system32\MSVCP140D.dll
File: c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.25017\include\xstring
Line: 2944

Expression: string subscript out of range

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)

加密功能

int encrypt(string plainText, string cipherText, int length, int key)
{
    int charLength = (int)plainText.length();
    if (key < 0)
    {
        return -1;
    }

    for (int i = 0; i < charLength; i++)
    {

        if (isalpha(plainText[i]))
        {
            plainText[i] = tolower(plainText[i]);
            cipherText[i] = plainText[i];

            for (int j = 0; j < key; j++)
            {

                if (cipherText[i] == 'z')
                {
                    cipherText[i] = 'a';
                }

                cipherText[i]++;
            }
        }
        
    }

    return charLength;
}

【问题讨论】:

标签: c++ visual-studio-2017 assertion caesar-cipher


【解决方案1】:

在写入之前,您永远不会检查 cipherText 的大小。你确定它的尺寸合适吗?鉴于您遇到的错误以及我们获得的信息很少,这就是原因。

尝试添加

cipherText.resize(charLength);

就在int charLength = (int)plainText.length();之后

【讨论】:

  • 我的断言失败消失了!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-26
  • 1970-01-01
相关资源
最近更新 更多