【发布时间】: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