2014-10-20 62 views
-1

我试图做输入字符串,这意味着回文检查的实例之后调用:终止抛出out_of_range

`"Anna"` returns `true`, 
`"ada"` returns `true`, 
`"1ada1"` return `true` 

"ABJKkjBa"返回true,

空间和符号,如” [。 ];”不算数。只比较字母和数字。

这里是我的程序:

int main() 
{ 
    string input; 
    getline(cin,input); 
    if(isPalindrome(input)) 
     cout << "it is palindrome phase, or words." << endl; 
    else 
     cout << "it's not palindrome phase, or words." << endl; 
    return 0; 
} 

bool isPalindrome(string input) 
{ 
    string TemStore_1; //only letter, number will be store in here. 
    string TemStore_2; //storing TemStore_1 data other way around. 
    for(int i=0;i<input.length();i++) 
    { 
     if((input.at(i)<58) && (input.at(i)>47)) //catching number from 0-9 
      TemStore_1+=input.at(i); 
     if((input.at(i))<123 && (input.at(i)>96)) //catching letter from a-z 
      TemStore_1+=input.at(i); 
     if((input.at(i)<91) && (input.at(i)>64)) //catching letter from A-Z, and change it to a-z 
     { 
      input.at(i)+=32; 
      TemStore_1+=input.at(i); 
     } 
    } 
    for(int j=TemStore_1.length();j>-1;j--) //backwards writing the TemStore_1 into TEmStore_2 
    { 
     TemStore_2+=TemStore_1.at(j); 
    } 
    if(TemStore_1==TemStore_2) 
     return true; 
    else 
     return false; 
} 

在我得到了超出范围的错误结束...不知道哪个部分出了问题..

+0

可以请你发布错误,因为我明白了吗? – 2014-10-20 20:31:23

+0

因此,可能在(i)或在(j)正在访问它不应该的东西? – 2014-10-20 20:31:30

+3

所以在调试器中运行它,找出抛出异常和超出范围的东西,找出它超出范围的原因,并弄清楚它是如何到达那里的。我们不需要。 – chris 2014-10-20 20:34:34

回答

0

这个循环:

for(int j=TemStore_1.length();j>-1;j--) 

起始于TemStore_1.length(),与TemStore_1.at(j)一起使用时会导致out_of_range

这是我注意到的第一个错误;它可能不是您的代码中唯一的错误。

+0

我不能让第二个循环,int j等于TemStore_1长度? – 2014-10-20 21:05:15

+0

啊..我的意思..是的,我现在解决了.. thx的帮助格雷格.. – 2014-10-20 21:10:40

相关问题