使用 Microsoft VS 在 C++ 中编写程序,但我在此处收到调试断言消息。它可以在 cpp.sh 和 repl.it 上正常运行,但不能在 VS 上运行。我能做什么?

Writing program in C++ using Microsoft VS, but I get a debug assertion message here. It runs on cpp.sh and repl.it fine, but not on VS. What can I do?

此函数用于从 char 数组中删除所有特殊字符、数字和空格。

// Michael E. Torres II
// Vigenere Cipher
// February 4, 2018

// C++ code to implement Vigenere Cipher
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <cctype>
#include <iterator>
#include <sstream>
#include <functional>
using namespace std;

// This function generates the key in
// a cyclic manner until it's length isi'nt
// equal to the length of original text
string generateKey(string str, string key)
{
    int x = str.size();

    for (int i = 0; ; i++)
    {
        if (x == i)
            i = 0;
        if (key.size() == str.size())
            break;
        key.push_back(key[i]);
    }
    return key;
}

// This function returns the encrypted text
// generated with the help of the key
string cipherText(string str, string key)
{
    string cipher_text;

    for (int i = 0; i < str.size(); i++)
    {
        // converting in range 0-25
        int x = (str[i] + key[i]) % 26;

        // convert into alphabets(ASCII)
        x += 'A';

        cipher_text.push_back(x);
    }
    return cipher_text;
}

// This function decrypts the encrypted text
// and returns the original text
string originalText(string cipher_text, string key)
{
    string orig_text;

    for (int i = 0; i < cipher_text.size(); i++)
    {
        // converting in range 0-25
        int x = (cipher_text[i] - key[i] + 26) % 26;

        // convert into alphabets(ASCII)
        x += 'A';
        orig_text.push_back(x);
        transform(orig_text.begin(), orig_text.end(), orig_text.begin(), ::tolower);
    }
    return orig_text;
}

string removeNonAlpha(char *str)
{
    unsigned long i = 0;
    unsigned long j = 0;
    char c;

    while ((c = str[i++]) != '[=12=]')
    {
        if (isalpha(c))  // this is where the breakpoint is automatically placed
        {
            str[j++] = c;
        }
    }
    str[j] = '[=12=]';
    return str;

}

// Driver program to test the above function
int main(int argc, char *argv[])
{
    string keyword = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
    stringstream ss;
    char a[] = "“I think and think for months and years. Ninety-nine times, the conclusion is false. The hundredth time I am right.” – Albert Einstein “Imagination is more important than knowledge. For knowledge is limited, whereas imagination embraces the entire world, stimulating progress, giving birth to evolution.” – Albert Einstein";
    int i = 0;

  string str = removeNonAlpha(a);

    str.append(512 - str.length(), 'X');

    transform(str.begin(), str.end(), str.begin(), ::toupper);


    transform(keyword.begin(), keyword.end(), keyword.begin(), ::toupper);


    string key = generateKey(str, keyword);
    string cipher_text = cipherText(str, key);

    transform(cipher_text.begin(), cipher_text.end(), cipher_text.begin(), ::tolower);
    transform(key.begin(), key.end(), key.begin(), ::tolower);

    string orig = originalText(cipher_text, key);

    cout << "Original/Decrypted Text : " << "\n";

    for (int i = 0; i < orig.size(); i += 81)
        orig.insert(i, "\n");
    cout << orig;

    cout << "\n\n" << "Ciphertext : " << "\n";

    for (int i = 0; i < cipher_text.size(); i += 81)
        cipher_text.insert(i, "\n");

    cout << cipher_text;

    cout << "\n\nPress ENTER key to Continue\n";
    getchar();

    return 0;
}

只要没有特殊字符 [.,%$@!^],char 数组在这个 while 循环中工作得很好。一旦 char 数组中有任何特殊字符,它就会给我调试断言:

"程序:...\Projects\ConsoleApplication17\Debug\ConsoleApplication17.exe 文件:minkernel\crts\ucrt\src\appcrt\convert\isctype.cpp 行:42

表达式:c >= -1 && c <= 255

...

程序“[11048] ConsoleApplication17.exe”已退出,代码为 3 (0x3)。

如果我在 repl.it 或 cpp.sh 上 运行 这个,我没有遇到任何问题。我感谢任何帮助。谢谢。

根本没有完成。它需要清理很多,但我只是想按原样对其进行测试。

https://msdn.microsoft.com/en-us/library/xt82b8z8.aspx isalpha 需要一个介于 0 和 0xFF 之间的数字:

The behavior of isalpha and _isalpha_l is undefined if c is not EOF or in the range 0 through 0xFF, inclusive. When a debug CRT library is used and c is not one of these values, the functions raise an assertion.

在传递给 isalpha 之前,您需要将 char 转换为 unsigned char。