// 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));”。不确定如何解决此错误。我一直在盯着它看几个小时没有。我搜索了互联网和这个网站,但找不到解决方案。请帮忙。
答案 0 :(得分:0)
我尝试调试你的代码,并在第99行抛出异常:c2 = table.at(ii).find(letters.at(2));
,而不是86.实际上,在第99行,你正在访问索引2的字母,但是字母的大小为2,所以是的这是一个错误。