【问题标题】:C++ Runtime Terminates std::out_of_range [closed]C++ 运行时终止 std::out_of_range [关闭]
【发布时间】:2016-07-15 03:39:33
【问题描述】:

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

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

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

下一步是询问加密/解密代码,当我输入出现错误的地方时

这是完整的代码:

#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;

}

抱歉第一部分的格式,我没有足够的声誉来发布多个链接。

解密部分工作正常。

错误是:

终止调用 std::out_of_range

【问题讨论】:

  • 对不起,你不会得到这份工作。
  • 他们在 stackoverflow.com 上找到了这篇文章。

标签: c++ c++11 encryption crash


【解决方案1】:

替换:

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

通过

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

【讨论】:

  • 先生,你不知道你帮了我多少,我希望我能回报你,但我没有信用卡。这正是我所需要的,一个奇迹,而您先生是个奇迹创造者。你应该为自己感到骄傲。
【解决方案2】:

C++ 中的数组和向量索引从 0 到 size - 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,这将返回encrypt.size()。但是,使用std::map. 实现整个事情可能会更好

性病,哈哈

【讨论】:

  • 抱歉格式不正确。
  • 下面有一个更简单的解释。
【解决方案3】:

查看this 网站以获取有关 std::func 中向量的更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 2017-03-08
    相关资源
    最近更新 更多