在抛出 'std::out_of_range' 实例后终止调用
terminate called after throwing an instance of 'std::out_of_range'
// 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));"。不确定如何解决此错误。我已经摆弄它好几个小时了。我搜索了互联网和这个网站,找不到解决方案。请帮助。
我试着调试你的代码,在第99行抛出异常:c2 = table.at(ii).find(letters.at(2));
,而不是86。实际上,在你的第99行你访问了字母的索引2,但是字母的大小是2,所以这是一个错误
相关文章:
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 我收到以下错误:抛出'std::bad_alloc'实例后终止调用
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- 在抛出 'std::runtime_error' 的实例后终止调用 what(): Filebuf 和 ostream 的 I/O 错误
- 在字符串函数中抛出'char const*'实例后调用的终止
- 在抛出 what() 的实例后调用'std::logic_error'终止:basic_string::_M_construct 空无效
- 抛出实例'std::out_of_range'后调用的终止
- 在抛出 'std::invalid_argument' 的实例后终止调用 what(): stoi (找不到问题所在)
- 在抛出"std::out_of_range"的实例后调用的 C++ 终止 std::vector
- C++程序问题:抛出'std::invalid_argument'实例后终止调用
- 在使用标准向量函数时引发'std::bad_alloc'实例后调用的终止
- 获取错误:在抛出"std::bad::alloc"的实例后终止调用 what(): std::bad_alloc
- 在抛出 'std::bad_weak_ptr' 的实例后终止调用 what(): bad_weak_ptr?
- 抛出 'std::logic_error' basic_string::_S_construct null 无效的实例后调用的终止
- "if (argc < 2 || argc > 2)"应该有 2 个参数吗?&终止在抛出'std::out_of_range'错误实例后调用
- 在抛出 'std::regex_error' 的实例后调用终止 what(): 括号未关闭
- 在抛出 'std::invalid_argument' 的实例后调用终止 what(): stoi 错误 in leet
- 调试"在抛出 ..) 实例后终止调用",当异常 _should_ 被捕获时
- 如何使用 H实例终止应用程序