gpt4 book ai didi

c++ - C++密码问题

转载 作者:行者123 更新时间:2023-12-02 10:01:54 25 4
gpt4 key购买 nike

我对编码世界是陌生的,并且一直在尝试用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;
}

我收到的错误-

terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::at: __n (which is 88) >= this->size() (which is 52)

最佳答案

使用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”功能时出现问题。

您的错误显示为

terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::at: __n (which is 88) >= this->size() (which is 52)



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

What you need to do is change "c" to "position".

关于c++ - C++密码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62146806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com