【问题标题】:C++ Cypher issueC ++密码问题
【发布时间】:2020-06-02 16:06:24
【问题描述】:

我是编码世界的新手,并且一直在尝试用 c++ 制作密码,但它抛出了一个错误,我不知道如何修复它。帮助将不胜感激,这是代码

#include <iostream>                                               
#include <string>

using namespace std;


int main(){

/* Variables Declaration! */


string alphabets{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};   

string key{"XWZYLMNOPQRSTUVABCDEFGHIJKuvwxyzabcdefghijklmnopqrst"};

string input{};

string encrypted_message{};

string decrypted_message{};

/*----------------------------------------------------------------------------------------------*/

cout << "Welcome To the Cypher!" << endl;
cout << "-------------------------------------------------------------" << endl;



cout << "Enter the code: " << endl;
getline(cin,input);


/*Encryption Part Code*/ 

cout << "Encrypting!"<< endl;
cout << endl ;

for (char c:input) {

    size_t postition = alphabets.find(c);
    if (postition != string::npos){
        encrypted_message += key.at(postition);
    }
    else 
        encrypted_message += c;
}   
cout << endl ;
cout << encrypted_message << endl;


/* Decryption Part Code */

cout << "Decrypting!" << endl;
cout << endl ;

for (char c : encrypted_message) {

    size_t position = key.find(c);

    if (position != string::npos){
        decrypted_message += alphabets.at(c);
    } 

    else
        decrypted_message += c;

}   
/* --------------------------------------------------------------------------------------*/    



cout << endl;
cout << "-----------------------------------------------------------------------------" ;
cout << endl ;

return 0;
}

我遇到的错误 -

在抛出 'std::out_of_range' 的实例后调用终止
what(): basic_string::at: __n (即 88) >= this->size() (即 52)

【问题讨论】:

  • 这是凯撒密码吗?错误是什么?
  • 这不是凯撒密码。
  • 理论是什么,错误信息是什么?
  • 此程序将您的文本作为输入,遍历每个字母,将其与字母变量中的字母进行比较,如果匹配,则返回它所在的位置,然后获取该位置并取出来自 key 变量的那个位置的字符,然后把它放在 encryted_message 变量中,这个过程一直重复,直到你的输入完全加密,然后它用我们转换它的相同过程解密 encrypted_message。
  • 错误消息:在抛出 'std::out_of_range' 的实例后调用终止 what(): basic_string::at: __n (即 88) >= this->size() (即52)

标签: c++ encryption


【解决方案1】:

使用 gdb 打印进程转储的回溯,

    (gdb) bt
    #0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
    #1  0x00007ffff7aed535 in __GI_abort () at abort.c:79
    #2  0x00007ffff7eb5943 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
    #3  0x00007ffff7ebb8a6 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
    #4  0x00007ffff7ebb8e1 in std::terminate() () from /lib/x86_64-linux-gnu/libstdc++.so.6
    #5  0x00007ffff7ebbb14 in __cxa_throw () from /lib/x86_64-linux-gnu/libstdc++.so.6
    #6  0x00007ffff7eb7851 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
    #7  0x00007ffff7f4779f in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::at(unsigned long) () from /lib/x86_64-linux-gnu/libstdc++.so.6
    #8  0x000055555555559b in main () at cypher.cc:61

根据#8,第61行有问题。

decrypted_message += alphabets.at(c);

根据#7,当您尝试“at”函数时出现问题。

你的错误是,

在抛出 'std::out_of_range' 的实例后调用终止 what(): basic_string::at: __n (即 88) >= this->size() (即 52)

这意味着您正在尝试访问超过字母 (52) 大小的 n (88)。

你需要做的是将“c”改为“position”。

【讨论】:

    【解决方案2】:

    您看到的错误是由该行引起的

    decrypted_message += alphabets.at(c);
    

    您使用一个字符进行索引,因此如果 encrypted_message 中的第一个字符是 X(在 ascii 中等于 88),它会尝试访问索引为 88 的元素,这是越界。然后.at() 会抛出异常。


    您的意图是使用position

    if (position != string::npos){
        // decrypted_message += alphabets.at(c);
        decrypted_message += alphabets.at(position);
    }
    

    使用调试器单步执行代码可以更轻松地找到这些错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-26
      • 2014-12-27
      • 1970-01-01
      • 2015-06-20
      相关资源
      最近更新 更多