C++ 运行时终止 std::out_of_range

C++ Runtime Terminates std::out_of_range

该程序应该询问用户 3 个选项:

1 用于加密 2 用于解密 3 退出

对于加密,第一步是要求加密纯文本,程序通过了。

下一步是询问 Encryption/Decryption 代码,当我输入一些错误出现的地方时

完整代码如下:

#include bits/stdc++.h

#include stdio.h 


using namespace std;

void encrypt001();

void decrypt001();


string yes;

string inname;

string key;

string alphabets="abcdefghijklmnopqrstuvwxyz";

int size=0;

int y;

int main()

{

    int useroption;

    do

    {
        cout<<"*****ENIGMA****"<<endl;
        cout<<"[1] - Encrypt"<<endl;
        cout<<"[2] - Decrypt"<<endl;
        cout<<"[3] - Exit"<<endl;
        cout<<"Enter Choice:"<<endl;
        cin>>useroption;

        switch (useroption)
        {
            case 1:
                encrypt001();
                break;
            case 2:
                decrypt001();
                break;
            default:
                cout<<"Exit"<<endl;
                break;



        }

    }


    while (useroption!=3);

}



void encrypt001()


{


    cout<<"*****ENCRYPTION******"<<endl;


    string encrypt;

    string ekey;
    cin.ignore();

    cout<<"Enter Plain Text To Encrypt:"<<endl;
    getline(cin,encrypt);
    cout<<"Enter Encryption Key:"<<endl;
    cin>>ekey;

    int elen=encrypt.length();
    int ekeylen=ekey.length();
    int letterslen=alphabets.length();
    int num;

    int y=0;

    for (int x=0; x<elen; x++)
    {
        for (int h=0; h<letterslen; h++)
        {
            if (ekey.at(y)==alphabets[h])
            {
                num=h;
            }
        }


    int num1=0;
    int num2=0;

    string space;
    space=encrypt.at(x);

    if (space==" ")
    {
        continue;
    }

    for (int j=0; j<letterslen; j++)
    {
        if (encrypt[x]==alphabets[j])
        {
            num1=j;
        }
    }
    num2=num+num1;

    if (num2>25)
    {
        num2=num2-26;
    }

    string letterrep;
    letterrep=alphabets.at(num2);
    encrypt.replace(x, 1, letterrep);

    y++;

    if (y>ekeylen)
    {
        y=0;
    }
    }
    cout<<"Encrypted Cipher Text:"<<encrypt<<endl;
    cout<<"Save To File? (y/n)"<<endl;
    cin>>yes;

    if (yes=="y")
    {
        cout<<"Enter File Name:"<<endl;
        cin>>inname;
        cout<<inname<<".txt saved succcessfully."<<endl;        
    }
    else
    {
        cout<<"Proceed"<<endl;
    }
}

void decrypt001()
{
    cout<<"*****DECRYPTION*****"<<endl;

    string decrypt;
    string dkey;
    string output;
    cin.ignore();

    cout<<"Enter Cipher Text to Decrypt:"<<endl;
    getline(cin, decrypt);
    cout<<"Enter Decryption Key:"<<endl;
    cin>>dkey;

    int dlen=0;
    int dkeylen=0;

    for (int k=0; k<decrypt.length(); k++)
    {
        if (decrypt[dlen]!=' ')
        {
            output+=((((decrypt[dlen]-97)+26)-(dkey[dkeylen]-97))%26)+97;
            dkeylen++;
            dlen++;
            if (dkeylen==dkey.length())
            {
                dkeylen=0;
            }
        }
        else if (decrypt[dlen]==' ')
        {
            output+=" ";
            dlen++;
        }
    }
    cout<<"Decrypted Plain Text:"<<output<<endl;

}

对于第一部分的格式化,我很抱歉,我没有足够的声誉来 post 多个链接。

解密部分工作正常。

错误是:

terminate called std::out_of_range

查看 this 网站了解有关 std::func 中矢量的更多信息。

替换:

if (y>ekeylen)
{
    y=0;
}

来自

if (y>=ekeylen)
{
    y=0;
}

C++ 中的数组和向量索引 运行 从 0 到大小 - 1。因此当你说

Int ellen = encrypt.Length

以后

if (y>ekeylen)
{
    y=0;
}

您正在尝试访问一个超过矢量加密末尾的文件。然后 at 成员函数抛出一个从未被捕获的 std::out_of_range 类型的异常,你的程序因此终止。

看起来你想找到与加密输入对应的向量索引;你可以这样做。 std::find and std::distance如下:

num = std::distance(planet.begin(), std::find(encrypt.begin(), encrypt.end(), c0Encrypt)); 如果找不到 Encrypt,这将 return encrypt.size()。但是,用 std::map.

来实现整个事情可能会更好

性病,笑