我似乎遇到類似於這裏的帖子:Confusing "std::out_of_range" Error。我的問題是我無法弄清楚我需要怎樣解決問題。這是我的代碼,我將在下面發佈錯誤。C++ Visual Studio超出範圍錯誤
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//In this program we are going to make a secret code decoder.
int main() {
/*char a = '!';
char b = '^';
char c = '&';
char d = '*';
char e = '@';
char f = '(';
char g = ')';
char h = '-';
char i = '#';
char j = '_';
char k = '=';
char l = '+';
char m = '[';
char n = '{';
char o = '$';
char p = ']';
char q = '}';
char r = ';';
char s = ':';
char t = ',';
char u = '%';
char v = '<';
char w = '.';
char x = '>';
char y = '/';
char z = '?';*/
// Now for the tedious part... declaring variables for the 26 letters of the alphabet, and searching the string and replacing... :p
int A = 0;
int B = 0;
int C = 0;
int D = 0;
int E = 0;
int F = 0;
int G = 0;
int H = 0;
int I = 0;
int J = 0;
int K = 0;
int L = 0;
int M = 0;
int N = 0;
int O = 0;
int P = 0;
int Q = 0;
int R = 0;
int S = 0;
int T = 0;
int U = 0;
int V = 0;
int W = 0;
int X = 0;
int Y = 0;
int Z = 0;
// Now getting the code itself.
string toDecode = "";
cout << "Enter code: ";
cin >> toDecode;
cout << endl;
//Now I should be searching the code that was entered to find my various symbols.
A = toDecode.find('!');
if (A != -1) {
toDecode.insert('!', 1, 'a');
return 0;
}
B = toDecode.find('^');
if (B != -1) {
toDecode.insert('^', 1, 'b');
return 0;
}
C = toDecode.find('&');
if (C != -1) {
toDecode.insert('&', 1, 'c');
return 0;
}
D = toDecode.find('*');
if (D != -1) {
toDecode.insert('*', 1, 'd');
return 0;
}
E = toDecode.find('@');
if (E != -1) {
toDecode.insert('@', 1, 'e');
return 0;
}
F = toDecode.find('(');
if (F != -1) {
toDecode.insert('(', 1, 'f');
return 0;
}
G = toDecode.find(')');
if (G != -1) {
toDecode.insert(')', 1, 'g');
return 0;
}
H = toDecode.find('-');
if (H != -1) {
toDecode.insert('-', 1, 'h');
return 0;
}
I = toDecode.find('#');
if (I != -1) {
toDecode.insert('#', 1, 'i');
return 0;
}
J = toDecode.find('_');
if (J != -1) {
toDecode.insert('_', 1, 'j');
return 0;
}
K = toDecode.find('=');
if (K != -1) {
toDecode.insert('=', 1, 'k');
return 0;
}
L = toDecode.find('+');
if (L != -1) {
toDecode.insert('+', 1, 'l');
return 0;
}
M = toDecode.find('[');
if (M != -1) {
toDecode.insert('[', 1, 'm');
return 0;
}
N = toDecode.find('{');
if (N != -1) {
toDecode.insert('{', 1, 'n');
return 0;
}
O = toDecode.find('$');
if (O != -1) {
toDecode.insert('$', 1, 'o');
return 0;
}
P = toDecode.find(']');
if (P != -1) {
toDecode.insert(']', 1, 'p');
return 0;
}
Q = toDecode.find('}');
if (Q != -1) {
toDecode.insert('}', 1, 'q');
return 0;
}
R = toDecode.find(';');
if (R != -1) {
toDecode.insert(';', 1, 'r');
return 0;
}
S = toDecode.find(':');
if (S != -1) {
toDecode.insert(':', 1, 's');
return 0;
}
T = toDecode.find(',');
if (T != -1) {
toDecode.insert(',', 1, 't');
return 0;
}
U = toDecode.find('%');
if (U != -1) {
toDecode.insert('%', 1, 'u');
return 0;
}
V = toDecode.find('<');
if (V != -1) {
toDecode.insert('<', 1, 'v');
return 0;
}
W = toDecode.find('.');
if (W != -1) {
toDecode.insert('.', 1, 'w');
return 0;
}
X = toDecode.find('>');
if (X != -1) {
toDecode.insert('>', 1, 'x');
return 0;
}
Y = toDecode.find('/');
if (Y != -1) {
toDecode.insert('/', 1, 'y');
return 0;
}
Z = toDecode.find('?');
if (Z != -1) {
toDecode.insert('?', 1, 'z');
return 0;
}
string beenDecoded = "";
/*Now at this point, we need to be able to identify the code when we see it. Let's use the space and type it all out here:
'!' > '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'
*/
// Now we have our code. Let's declare a variable for each of those, and set them to equal.
// Let's get the message printed:
cout << toDecode << endl;
return 0;
}
現在的錯誤:
// exception handling support functions
#include <new>
#include <stdexcept>
_STD_BEGIN
_CRTIMP2_PURE _NO_RETURN(__CLRCALL_PURE_OR_CDECL _Xbad_alloc())
{ // report a bad_alloc error
_THROW_NCEE(_XSTD bad_alloc, _EMPTY_ARGUMENT);
}
_CRTIMP2_PURE _NO_RETURN(__CLRCALL_PURE_OR_CDECL _Xinvalid_argument(_In_z_ const char *_Message))
{ // report an invalid_argument error
_THROW_NCEE(invalid_argument, _Message);
}
_CRTIMP2_PURE _NO_RETURN(__CLRCALL_PURE_OR_CDECL _Xlength_error(_In_z_ const char *_Message))
{ // report a length_error
_THROW_NCEE(length_error, _Message);
}
_CRTIMP2_PURE _NO_RETURN(__CLRCALL_PURE_OR_CDECL _Xout_of_range(_In_z_ const char *_Message))
{ // report an out_of_range error
_THROW_NCEE(out_of_range, _Message);
}
_CRTIMP2_PURE _NO_RETURN(__CLRCALL_PURE_OR_CDECL _Xoverflow_error(_In_z_ const char *_Message))
{ // report an overflow error
_THROW_NCEE(overflow_error, _Message);
}
_CRTIMP2_PURE _NO_RETURN(__CLRCALL_PURE_OR_CDECL _Xruntime_error(_In_z_ const char *_Message))
{ // report a runtime_error
_THROW_NCEE(runtime_error, _Message);
}
_STD_END
#include <functional>
_STD_BEGIN
_CRTIMP2_PURE _NO_RETURN(__CLRCALL_PURE_OR_CDECL _Xbad_function_call())
{ // report a bad_function_call error
_THROW_NCEE(bad_function_call, _EMPTY_ARGUMENT);
}
_STD_END
#if _HAS_EXCEPTIONS
#include <regex>
_STD_BEGIN
_CRTIMP2_PURE _NO_RETURN(__CLRCALL_PURE_OR_CDECL _Xregex_error(regex_constants::error_type _Code))
{ // report a regex_error
_THROW_NCEE(regex_error, _Code);
}
_STD_END
#endif /* _HAS_EXCEPTIONS */
任何幫助表示讚賞!
太棒了!這解決了錯誤。現在我執行代碼,只要我輸入我想要翻譯的任何內容,它就會毫無錯誤地運行,但它只是退出而不顯示任何內容。 @Naren Neelamegam你需要我發佈修改後的代碼嗎? – Danny
如果條件使得程序在更換字符後退出,則返回「0」。用一個簡單的方法更新答案。請檢查。另外,請參閱C++函數參考以更好地瞭解如何有效地使用它們。 –
OH。謝謝 - 我認爲它現在正在工作! – Danny