我正在编写一个代码来查找回文数。我正在使用它来学习如何编码,这是我编写的代码:
#include <iostream>
#include <sstream>
using namespace std;
bool IsPalindromic (int n) {
int I;
string Number; //Starting to convert integer to string
ostringstream convert;
convert << n;
Number = convert.str(); //Conversion is complete
int M = Number.length();
bool Result = true;
if (M % 2 == 0)
I = M / 2;
else
I = (M - 1)/2;
for (int i = 1; i <= I; i++) {
if (Number.at(i) != Number.at(M-i+1))
Result = false;
}
return Result;
}
int main(int argc, char *argv[]) {
int num;
cout << "Enter an integer:";
cin >> num;
cout << IsPalindromic(num) << endl;
return 0;
}
但是当我尝试运行它并输入一个整数作为输入时,我收到以下错误:
libc++abi.dylib: terminate called throwing an exception
Abort trap: 6
我在代码逻辑中找不到任何错误。你能帮我找出问题吗?我在 Mac OSX 10.8.4 上使用 g++ 进行编译