Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在抛出';std::超出范围'; //头和名称空间 #包括 #包括 #包括 #包括 #包括 使用名称空间std; int main() { //创建原始表 向量表={“ABCDE”、“FGHIJ”、“KLMNO”、“PRSTU”、“VWXYZ”}; //获取用户输入 字符串消息,键; cout_C++_Encryption_Range_Std_Out - Fatal编程技术网

C++ 在抛出';std::超出范围'; //头和名称空间 #包括 #包括 #包括 #包括 #包括 使用名称空间std; int main() { //创建原始表 向量表={“ABCDE”、“FGHIJ”、“KLMNO”、“PRSTU”、“VWXYZ”}; //获取用户输入 字符串消息,键; cout

C++ 在抛出';std::超出范围'; //头和名称空间 #包括 #包括 #包括 #包括 #包括 使用名称空间std; int main() { //创建原始表 向量表={“ABCDE”、“FGHIJ”、“KLMNO”、“PRSTU”、“VWXYZ”}; //获取用户输入 字符串消息,键; cout,c++,encryption,range,std,out,C++,Encryption,Range,Std,Out,我试图调试您的代码,但在第99行抛出异常:c2=table.at(ii).find(letters.at(2));,而不是86。事实上,在您的第99行中,您正在访问字母的索引2,但字母大小为2,所以是的,这是一个错误。不知道这个错误,但使用字符串作为字符集的方式是低效的。有std::set用于此。和std::bitset。请您的问题提供一个答案。任何未捕获的异常都会导致程序终止。是否使用任何try…catch构造? // Headers and namespace #include <io

我试图调试您的代码,但在第99行抛出异常:
c2=table.at(ii).find(letters.at(2));
,而不是86。事实上,在您的第99行中,您正在访问字母的索引2,但字母大小为2,所以是的,这是一个错误。

不知道这个错误,但使用字符串作为字符集的方式是低效的。有
std::set
用于此。和
std::bitset
。请您的问题提供一个答案。任何未捕获的异常都会导致程序终止。是否使用任何
try
catch
构造?
// Headers and namespace
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <string>

using namespace std;

int main()
{
    //Create original table
    vector<string> table = { "ABCDE", "FGHIJ", "KLMNO", "PRSTU", "VWXYZ" };

    //Get user input
    string msg,key;
    cout << "Enter encrypted message: ";
    // must use cin for strings not regular cin
    getline(cin,msg);
    cout << "Enter key: ";
    getline(cin,key);

    //Remove duplicate letters
    string newKey;
    // changed ii +
    for (int ii = 0; ii < key.size(); ii+= 1) {
        if ( count(newKey.begin(),newKey.end(),key.at(ii)) == 0 ) {
            newKey.push_back(key.at(ii));
        }
    }


    //Check which letters from table are not already in key
    //should be < not <= because it starts at 0
    for (int ii = 0; ii < 5; ii += 1) {
        for (int jj = 0; jj < 5; jj += 1) {
            if ( count(newKey.begin(),newKey.end(),table.at(ii).at(jj)) == 0 ) {
                newKey.push_back( table.at(ii).at(jj) );
            }
        }
    }

    //Create new table

    cout << "Updated table: " << endl;
    //kk must begin at 0 not 1
    int kk = 0;
        for (int ii = 0; ii < 5; ii += 1) {
                for (int jj = 0; jj < 5; jj += 1) {
                 table.at(ii).at(jj) = newKey.at(kk);
                 cout << table.at(ii).at(jj);
                 kk += 1;

        }
            //move end line to outer for loop to make table
            cout << endl;
    }


    //Copy original message
    // msg and decrypt in wrong order
    string decrypt = msg;

    //Process every letter in message in groups of two
    //counter must start at 1
    int cnt = 1;
    //change to <=
    while (cnt <= msg.size()-1) {
        //Look for two letters, skipping spaces/punctuation
        string letters;
        vector<int> spot;
        while (letters.size() < 2) {

            if ( isalpha( msg.at(cnt) ) ) {
                letters.push_back( toupper(msg.at(cnt)) );
                spot.push_back(cnt);
            }
            //missing ;
            cnt += 1;
        }

        //Find the row and columns of the two letters
        int r1 = 0,c1 = 0;
        for (int ii = 0; ii < 5; ii += 1) {
            //letters must start at 1
            c1 = table.at(ii).find(letters.at(1));
            if (c1 != string::npos ) {

                // order of lines switched so code r1=ii will be used
                r1 = ii;
                break;
            }

        }

        int r2 = 0,c2 = 0;
        for (int ii = 0; ii < 5; ii += 1) {
            //letters must start at 2
            c2 = table.at(ii).find(letters.at(2));
            if (c2 != string::npos ) {
                // order of lines switched so code r1=ii will be used
                r2 = ii;
                break;
            }

        }



        //If the letters are in the same row
        //switched contents of if and elseif statements
        if (r1 == r2) {
            c1 = c1-1;
            c2 = c2-1;
            //If in the same column
        } else if (c1 == c2) {
            r1 = r1-1;
            r2 = r2-1;
            //Different row/column combo
        } else {
            //must swap c2 with c1
            swap(c2,c1);
        }

        //Adjust r1,c1 and r2,c2 to make sure they don't off the table
        //all must be == not less than zero
        if (c1 == 0) { c1 = 5; }
        if (c2 == 0) { c2 = 5; }
        if (r1 == 0) { r1 = 5; }
        if (r2 == 0) { r2 = 5; }

        //Find new letters for pair
        decrypt.at(spot.at(1)) = table.at(r1).at(c1);
        decrypt.at(spot.at(2)) = table.at(r2).at(c2);

        //Correct for lowercase
        // missing beginning curly brace
        //spot must be at 1
        if ( islower(msg.at(spot.at(1))) ) {
            //spot must be at 1
            decrypt.at(spot.at(1)) = tolower( decrypt.at(spot.at(1)) );
        }
         //spot must be at 2
        if ( islower(msg.at(spot.at(2))) ) {
            //spot must be at 2
            decrypt.at(spot.at(2)) = tolower( decrypt.at(spot.at(2)) );
        }

    }

//Remove any X's
decrypt.erase(remove(decrypt.begin(),decrypt.end(),'X'),decrypt.end());

//Show secret message
cout << "Secret message: " << decrypt << endl;

return 0;
}