所以我正在编写一个代码,通过使用公式ai = letter((position(ai)+ position(ai + 1))mod k)将字母切换到其后的字母来加密和输入
其中k是字母表中字母的数目。

现在这是我的代码;

using namespace std;

class Encrypt {
private:
    char letters[27];
    char *String = new char[500];
    char letters_cipher[25];
    unsigned int i, k;
public:
    Encrypt() :letters{ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' } {

        for (unsigned int i = 0; i < strlen(String); i++) {
            String[i] = '\0';
        }
    }
    ~Encrypt() {
        delete[] String;
    }
    void GetString() {
        cout << "Enter String : ";
        std::cin >> String;
    }

    void encrypt() {
        for (i = 1; i <= 26; i++) { // Run 26 times (1 for each letter)
            letters_cipher[i] = letters[(i + (i + 1)) % 26];
            cout << letters_cipher[i] << endl;
        }
        for (i = 0; i <= (strlen(String) - 1); i++) { // individual characters of string x can be referenced by x[0] etc
            for (k = 0; k <= 25; k++) {
                if (String[i] == letters[k]) {
                    cout << letters_cipher[k];
                }
            }
        }
    }

    void Print() {
        for (unsigned int i = 0; i < strlen(letters_cipher); i++) {
            cout << letters_cipher[i];
        }
    }

};


我收到以下错误


  引发异常:读取访问冲突。
  this-> String为0x128F112。


对于该行:


  如果(String [i] ==字母[k])


知道我该如何解决吗?

编辑:

现在,我对代码进行了一些编辑,如下所示;

class Encrypt {
private:
    char letters[27];
    char *String = new char[500];
    char letters_cipher[27];
    unsigned int i, k;
public:
    Encrypt() :letters{ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' } {

        for (unsigned int i = 0; i < 500; i++) {
            String[i] = '\0';
        }
    }
    ~Encrypt() {
        delete[] String;
    }
    void GetString() {
        cout << "Enter String : ";
        std::cin >> String;
    }

    void encrypt() {
        for (i = 1; i <= 26; i++) { // Run 26 times (1 for each letter)
            letters_cipher[i] = letters[(i + (i + 1)) % 26];
            cout << letters_cipher[i] << endl;
        }
        for (i = 0; i <= (sizeof(String) - 1); i++) { // individual characters of string x can be referenced by x[0] etc
            for (k = 0; k <= 25; k++) {
                if (String[i] == letters[k]) {
                    cout << letters_cipher[k];
                }
            }
        }
    }

    void Print() {
        for (unsigned int i = 0; i < sizeof(letters_cipher); i++) {
            cout << letters_cipher[i];
        }
    }

};


错误不再存在,程序运行但由于错误而关闭;


  'ConsoleApplication5.exe'(Win32):已加载'C:\ Windows \ SysWOW64 \ kernel.appcore.dll'。找不到或打开PDB
  文件。 'ConsoleApplication5.exe'(Win32):已加载
  'C:\ Windows \ SysWOW64 \ msvcrt.dll'。找不到或打开PDB文件。
  'ConsoleApplication5.exe'(Win32):已加载
  'C:\ Windows \ SysWOW64 \ rpcrt4.dll'。找不到或打开PDB文件。
  'ConsoleApplication5.exe'(Win32):已加载
  'C:\ Windows \ SysWOW64 \ sspicli.dll'。找不到或打开PDB文件。
  'ConsoleApplication5.exe'(Win32):已加载
  'C:\ Windows \ SysWOW64 \ cryptbase.dll'。找不到或打开PDB文件。
  'ConsoleApplication5.exe'(Win32):已加载
  'C:\ Windows \ SysWOW64 \ bcryptprimitives.dll'。找不到或打开
  PDB文件。 'ConsoleApplication5.exe'(Win32):已加载
  'C:\ Windows \ SysWOW64 \ sechost.dll'。找不到或打开PDB文件。
  程序“ [20764] ConsoleApplication5.exe”已退出,代码为0
  (0x0)。

最佳答案



    for (i = 1; i <= 26; i++) { // Run 26 times (1 for each letter)
        letters_cipher[i] = letters[(i + (i + 1)) % 26];


您从letters_cipher中写入2个索引作为char letters_cipher[25];,则行为未定义

和在

       for (k = 0; k <= 25; k++) {
            if (String[i] == letters[k]) {
                cout << letters_cipher[k];
            }


您从letters_cipher中读取一个索引,则该行为再次未定义

letters_cipher中允许的最大索引为24

另请注意

   for (unsigned int i = 0; i < strlen(letters_cipher); i++) {
        cout << letters_cipher[i];
    }


strlen(letters_cipher)也不起作用,因为在代码中无处输入空结束符,因此strlen也会脱离letters_cipher的初始化部分,并且可能会再次出现。

您还存在以下问题:

    for (unsigned int i = 0; i < strlen(String); i++) {
        String[i] = '\0';
    }


因为您在没有初始化String的情况下执行strlen(String)第一个循环,所以将其替换为500(或者最好使用std :: string)


  知道我该如何解决吗?


通常,不要在循环中使用26或25之类的数字,而应使用sizeof,并且您也可以使用std :: vector而不是(C)数组。当内容恒定时,请勿自行调整大小,因此对于letters只需执行以下操作:

static const char letters_cipher[] = {
   'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
   'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
   'y', 'z'
};


可能letters_cipher的大小必须与letters相同,并更正该设置,因为for始于1而不是0

补充说明


数组letters是无用的,它仅包含字母a..z,因此letters[i]只是'a' + i
您同时执行using namespace std;std::xxx,请选择其中之一(很多人会说您不执行using namespace std;




一个建议,如果我很好理解您的文字:

#include <iostream>
#include <string>

class Encrypt {
  private:
    std::string str;
    char letters_cipher['z' - 'a' + 1];

    inline char letter(size_t i) { return 'a' + i; } // to help
  public:
    Encrypt() {} // default definition is useless in fact
    ~Encrypt() {} // default definition is useless in fact

    bool Getstr() {
      std::cout << "Enter str : ";
     return (std::cin >> str); // to indicates EOF in case
    }

    void encrypt() {
      for (size_t i = 1; i <= sizeof(letters_cipher); i++) { // Run 26 times (1 for each letter)
        letters_cipher[i - 1] = letter((i + (i + 1)) % 26);
        std::cout << letters_cipher[i - 1] << std::endl;
      }
      for (size_t i = 0; i < str.length(); i++) { // individual characters of string x can be referenced by x[0] etc
        for (size_t k = 0; k < sizeof(letters_cipher); k++) {
          if (str[i] == letter(k)) {
            std::cout << letters_cipher[k];
          }
        }
      }
      std::cout << std::endl;
    }

    void Print() {
      for (size_t i = 0; i < sizeof(letters_cipher); i++) {
        std::cout << letters_cipher[i];
      }
      std::cout << std::endl;
    }
};

int main()
{
  Encrypt e;

  e.Getstr();
  e.encrypt();
  e.Print();
}


编译执行:

/tmp % g++ -pedantic -Wextra e.cc
/tmp % ./a.out
Enter str : azerty
d
f
h
j
l
n
p
r
t
v
x
z
b
d
f
h
j
l
n
p
r
t
v
x
z
b
dbllpz
dfhjlnprtvxzbdfhjlnprtvxzb


在valgrind下执行:

/tmp % valgrind ./a.out
==29157== Memcheck, a memory error detector
==29157== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==29157== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==29157== Command: ./a.out
==29157==
Enter str : azerty
d
f
h
j
l
n
p
r
t
v
x
z
b
d
f
h
j
l
n
p
r
t
v
x
z
b
dbllpz
dfhjlnprtvxzbdfhjlnprtvxzb
==29157==
==29157== HEAP SUMMARY:
==29157==     in use at exit: 0 bytes in 0 blocks
==29157==   total heap usage: 4 allocs, 4 frees, 115 bytes allocated
==29157==
==29157== All heap blocks were freed -- no leaks are possible
==29157==
==29157== For counts of detected and suppressed errors, rerun with: -v
==29157== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)

关于c++ - 引发未处理的异常:读取访问冲突。 this-> String为0x1C6F112,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55023731/

10-11 18:34