在C+中加密字符串+; 我非常需要帮助我的C++程序,该程序应该加密用户输入的字符串(源代码/代码>),并将加密的字符串保存到另一个字符串(目的地< /代码>)。

在C+中加密字符串+; 我非常需要帮助我的C++程序,该程序应该加密用户输入的字符串(源代码/代码>),并将加密的字符串保存到另一个字符串(目的地< /代码>)。,c++,C++,在加密字符串之前,它会运行lowercaseestring()函数,将source转换为所有小写。这个函数工作得非常好 你能帮助我的程序正确显示加密信息吗?我是C++新手。 我的错误: terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::at: __n (which is 0) >= this->size() (which is 0) #包括 #包括

在加密字符串之前,它会运行
lowercaseestring()
函数,将
source
转换为所有小写。这个函数工作得非常好

你能帮助我的程序正确显示加密信息吗?我是C++新手。

我的错误:

terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::at: __n (which is 0) >= this->size() (which is 0)
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
字符串小写字符串(字符串和源);
布尔替换(字符串和源、字符串密码键、字符串和目标);
int main()
{
字符串源;
字符串cipherKey=“qwertyuiopasdfghjklzxcvnm”;
字符串目的地;
河流充填;
声源;
//最终必须写入文件,但我想先让密码部分工作
//cin>>密码匙;
//infle.open(“C:/Users/ellio/OneDrive/Desktop/Freshman semperson 2/ECE 1080C/ECE Labs/otherlabfiles/small file.txt”);/*对
//文件访问路径*/
//如果(!infle){

//cout以下是您的替换方法。目标字符串为空,导致错误:

bool substitution(string & source, string cipherKey, string &destination)
{
    string alphabet = "abcdefghijklmnopqrstuvwxyz";

    destination = source; // To make sure the size of destination is the same as source

    if(source.size() == 0)
    {
        return false;
    }
    else
    {
        for(unsigned int i = 0; i < source.size(); ++i) // You can declare i and j in the loop if you want (in c++ not in c)
        {
            for(unsigned int j = 0; j < alphabet.size(); ++j) // way easier to use size instead of finding 'z'
            {
                if(source.at(i) == alphabet.at(j))
                {
                    destination.at(i) = cipherKey.at(j);
                }
            }
        }
        return true;
    }


}
bool替换(字符串和源、字符串密码键、字符串和目标)
{
字符串字母表=“abcdefghijklmnopqrstuvxyz”;
destination=source;//确保目标的大小与源的大小相同
if(source.size()==0)
{
返回false;
}
其他的
{
对于(未签名int i=0;i <源代码)(siz()),(+ +i)/ /或可以在循环中声明i和j(如果C++中没有C)
{
for(unsigned int j=0;j
请注意,字符查找位可以减少为:

bool substitution(string & source, string cipherKey, string &destination)
{
    string alphabet = "abcdefghijklmnopqrstuvwxyz";

    destination = source; // To make sure the size of destination is the same as source

    if(source.size() == 0)
        return false;
    else
    {
        for(unsigned int i = 0; i < source.size(); ++i)
            destination.at(i) = cipherKey.at(alphabet.find(source.at(i)));
        return true;
    }
}
bool替换(字符串和源、字符串密码键、字符串和目标)
{
字符串字母表=“abcdefghijklmnopqrstuvxyz”;
destination=source;//确保目标的大小与源的大小相同
if(source.size()==0)
返回false;
其他的
{
for(无符号整数i=0;i
您试图访问目标,但它是空的。请尽量避免对特定索引进行操作。可以这样做:

void substituteCharacters(const std::string& source,
                          const std::string& cipherKey,
                          std::string& destination)
{
    destination.reserve(source.size());//allocate memory once, so no allocation during push_backs
    for(auto c : source)
        destination.push_back(cipherKey[c-'a']);
}

默认情况下,字符串为空。您不能分配给不存在的字符。开发环境附带的调试器可以帮助您快速解决此问题。您已经迈出了伟大的第一步,在
上使用
,并利用它为您检查边界。然后抛出异常。调试器将停止程序我死了。然后使用回溯挖掘代码,查看程序在哪里越界。检查播放中的数字,然后只需查看它们是如何被允许越界的。如果执行类似“在字符串加密之前…”的步骤,你对解密后返回原始字符串有什么希望?除此之外,异常告诉你什么?对不起,我在编程时非常慢。什么被认为是超出范围的?我迭代到一个小于源的大小(源的最后一个索引),我尝试了几个不同的条件对于第二个for循环,循环遍历字母表,这不会改变任何内容。cipherKey的数字与字母表的索引值相同,因此总体而言,我仍然非常困惑:/。此行
destination.at(I)=source.at(I);
尝试访问
目的地的索引0
。但是
目的地
为空,因此0超出范围。
void substituteCharacters(const std::string& source,
                          const std::string& cipherKey,
                          std::string& destination)
{
    destination.reserve(source.size());//allocate memory once, so no allocation during push_backs
    for(auto c : source)
        destination.push_back(cipherKey[c-'a']);
}