我正在创建一个程序来加密或解密消息。程序执行但返回 R6010 错误,所以我假设我给出的条件有问题。但是,在开始调用中止之前,程序会保持完美运行一两次。
我正在使用Visual Studio进行C++。仍处于起步阶段/难以理解其他R6010中止错误给出的一些解决方案。也许我的代码太草率了,但我希望得到一些建议来清理它,或者至少通过中止。谢谢。
从原始代码编辑;更正了数组大小,首先在循环中检查了溢出(如果找不到字符,则更正了中止错误(,将循环更改为 do-while 循环,以及在 if 语句上将 0 更改为 -1(do-while 让程序初始化为 i=0,在循环以 ++i 增量重新启动之前需要 -1(。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
//Initiate vectors, strings and variables.
vector<char> normalV(27);
vector<char> cipherV(27);
string toDec;
int i = 0, j = 0;
//Initiate string & decipher variables.
normalV.at(0) = 'a'; cipherV.at(0) = '!';
normalV.at(1) = 'b'; cipherV.at(1) = '^';
normalV.at(2) = 'c'; cipherV.at(2) = '&';
normalV.at(3) = 'd'; cipherV.at(3) = '*';
normalV.at(4) = 'e'; cipherV.at(4) = '@';
normalV.at(5) = 'f'; cipherV.at(5) = '(';
normalV.at(6) = 'g'; cipherV.at(6) = ')';
normalV.at(7) = 'h'; cipherV.at(7) = '-';
normalV.at(8) = 'i'; cipherV.at(8) = '#';
normalV.at(9) = 'j'; cipherV.at(9) = '_';
normalV.at(10) = 'k'; cipherV.at(10) = '=';
normalV.at(11) = 'l'; cipherV.at(11) = '+';
normalV.at(12) = 'm'; cipherV.at(12) = '[';
normalV.at(13) = 'n'; cipherV.at(13) = '{';
normalV.at(14) = 'o'; cipherV.at(14) = '$';
normalV.at(15) = 'p'; cipherV.at(15) = ']';
normalV.at(16) = 'q'; cipherV.at(16) = '}';
normalV.at(17) = 'r'; cipherV.at(17) = ';';
normalV.at(18) = 's'; cipherV.at(18) = ':';
normalV.at(19) = 't'; cipherV.at(19) = ',';
normalV.at(20) = 'u'; cipherV.at(20) = '%';
normalV.at(21) = 'v'; cipherV.at(21) = '<';
normalV.at(22) = 'w'; cipherV.at(22) = '.';
normalV.at(23) = 'x'; cipherV.at(23) = '>';
normalV.at(24) = 'y'; cipherV.at(24) = '/';
normalV.at(25) = 'z'; cipherV.at(25) = '?';
//Get user to input string
cout << "Input message to encrypt/decrypt: " << endl;
getline(cin, toDec);
//creates loop to check for each index of
do {
//cout << toDec.at(i) << endl;
//output if character not in encryption/decryption vectors.
if (i == 26) {
cout << toDec.at(j);
i = -1;
j++;
}
// Encryption
else if (normalV.at(i) == toDec.at(j)) {
cout << cipherV.at(i);
i = -1;
j++;
}
// Decryption
else if (cipherV.at(i) == toDec.at(j)) {
cout << normalV.at(i);
i = -1;
j++;
}
++i;
} while (j < toDec.length());
cout << endl;
return 0;
}
这是一个更简单的大纲:
#include <iostream>
#include <map>
using namespace std;
int main() {
std::map<char, char> encoding = {
{'a', '!'},
{'b', '^'},
{'c', '&'},
{'d', '*'},
{'e', '@'},
{'f', '('},
{'g', ')'},
{'h', '-'},
{'i', '#'},
{'j', '_'},
{'k', '='},
{'l', '+'},
{'m', '['},
{'n', '{'},
{'o', '$'},
{'p', ']'},
{'q', '}'},
{'r', ';'},
{'s', ':'},
{'t', ','},
{'u', '%'},
{'v', '<'},
{'w', '.'},
{'x', '>'},
{'y', '/'},
{'z', '?'} };
string toDec;
//Get user to input string
cout << "Input message to encrypt/decrypt: " << endl;
getline(cin, toDec);
for(char c : toDec )
{
cout << encoding.at(c);
}
return 0;
}