2015-10-14 113 views
0

我似乎遇到類似於這裏的帖子: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 */ 

任何幫助表示讚賞!

回答

1

當你調用insert()函數時,你的整個代碼看起來像你想要發生的事情,可能是用其他的替換字符!!!?

正如John正確地提到的,insert()函數沒有將第一個param作爲字符,它們是要插入字符或字符串的位置(int或iterators)。所有對insert()的調用都將第一個參數作爲一個字符,它給出它的ascii值來定位insert()函數的參數,因此您會超出範圍錯誤。

參考插件的語法()這裏string::insert()

string& insert (size_t pos, const string& str); 
string& insert (size_t pos, const string& str, size_t subpos, size_t sublen); 
string& insert (size_t pos, const char* s); 
string& insert (size_t pos, const char* s, size_t n); fill (5) string& insert (size_t pos, size_t n, char c); 
void insert (iterator p, size_t n, char c); 
insert (iterator p, char c); 
void insert (iterator p, InputIterator first, InputIterator last); 

正如我剛纔所說,它看起來像插入()是不是你期待在那裏你的目的的功能。你在尋找替換()的字符。然後使用,

string& replace (size_t pos, size_t len, size_t n, char c); 

然後再次,此功能需要位置作爲第一個參數。類似這樣的,

A = toDecode.find('!'); 
     if (A != -1) { 
     toDecode.replace(A, 1,1,'a'); 
     return 0; 
    } 

上面的替換(A,1,1,'a')函數調用將會替換第一個出現的'!' 'a'字符串中的字符。

請記住,在replace()函數中,第一個參數是位置,如果超過了字符串的長度,您將得到out_of_range。第二個參數是要替換的字符數,第三個是複製字符的次數,第四個是要放在字符串中的字符。

更新#1: 尋找到你的代碼遠一點,我的理解是,你實際上是試圖取代」之後^ & * @() - #_ = + [{$]} ;: ,%<。> /?「帶有「abcdefghijklmnopqrstuvwxyz」字符的字符。無論如何,你的「返回0」將會破壞它。我不知道你爲什麼這樣寫。但總的來說,程序可以這樣簡單,

#include <string> 
#include <iostream> 
#include <cstddef> 
using namespace std; 
int _tmain(int argc, _TCHAR* argv[]) 
{ 
    string actual("abcdefghijklmnopqrstuvwxyz"); 
    string codes ("!^&*@()-#_=+[{$]};:,%<.>/?"); 
    string toDecode =""; 
    cout << "Enter code:";   // get the string to decode 
    cin >> toDecode; 
    cout <<endl; 
    // find the code chars in the entered string 
    size_t found = toDecode.find_first_of(codes); 
    // loop through all the found code characters 
    while(found!=std::string::npos) 
    { 
     // get the position of the found code character in string "codes" 
     // the same position character in string actual is the decoded char 
     size_t cpos=codes.find(toDecode[found]); 
     // assign the decoded char to the code char. Eg: '!'='a' 
     toDecode[found]=actual[cpos]; 
     // Look for the next code character 
     found = toDecode.find_first_of(codes); 
    } 
    cout << toDecode; 
    return 0; 
} 
+0

太棒了!這解決了錯誤。現在我執行代碼,只要我輸入我想要翻譯的任何內容,它就會毫無錯誤地運行,但它只是退出而不顯示任何內容。 @Naren Neelamegam你需要我發佈修改後的代碼嗎? – Danny

+0

如果條件使得程序在更換字符後退出,則返回「0」。用一個簡單的方法更新答案。請檢查。另外,請參閱C++函數參考以更好地瞭解如何有效地使用它們。 –

+0

OH。謝謝 - 我認爲它現在正在工作! – Danny

0

沒有std :: string insert()接受參數char,int,char,所以你的字符被提升爲整數,因爲你的字符串是空的 - 也許你的意思是替換而不是插入?

我猜它最終會打電話給insert (size_t pos, size_t n, char c);。另一個提示是如果使用你需要的插入str = str.insert(...);

+0

你能給我一個「參數char,int,char」的例子,我不知道你在說什麼。 – Danny

+0

您可以調用'toDecode.insert('!',1,'a');'這不符合您的期望。它試圖在字符串的第33位插入1'a'(因爲'!'是十進制33)。該字符串沒有位置33. – John3136