-2
// 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;
}
それはライン86でエラーを投げているのインスタンス投げた後に呼び出さ終了 "のC1 = table.atを(II).find(letters.at(1));"。このエラーを解決する方法が不明です。私は数時間の間それをぶち壊すのを見つめていませんでした。私はインターネットとこのサイトを検索し、解決策を見つけることができませんでした。助けてください。'はSTD :: out_of_range'
エラーについてはわかりませんが、このコードのように文字列を文字セットとして使用すると効率が悪いです。これには 'std :: set'があります。そして 'std :: bitset'。 –
あなたの質問を[編集]して[mcve]を提供してください。 –
キャッチされていない例外が発生すると、プログラムが終了します。あなたは 'try' ...' catch'構文を使用していますか? – Robert