I'm writing a numerical palindromes program and am stuck on a function that sees if a particular number is a palindrome
Code:
bool checkIfPalindrome(int input)
{
const unsigned int inputLength = digits(input); // computes the number of digits in "input"
char input1[inputLength];
char input2[inputLength];
itoa(input, input1, 10); // turned input into an array of chars
int lastIndex = inputLength - 1;
int currentIndex = lastIndex;
for(int i = 0; currentIndex >= 0; i++, currentIndex--) //turns the second array into a mirror of the first
{
input2[i] = input1[currentIndex];
}
//now see if the 2 arrays are equal
for(int i = 0; i <= lastIndex; i++)
{
if(input1[i] != input2[i])
return false;
}
return true;
}
int digits(int n) //computes number of digits in an integer
{
int i;
if(n == 0)
return 1;
for(i = 0; n; ++i)
{
n /= 10;
}
return i;
}
and I'm getting the following errors:
Quote:
main.cpp(37) : error C2057: expected constant expression
main.cpp(37) : error C2466: cannot allocate an array of constant size 0
main.cpp(37) : error C2133: 'input1' : unknown size
plus the same errors for "input2"
the digits function should never return 0, not sure how to convince the compiler of that, plus the last error, I'm assuming that has to do with the program not knowing how much memory to allocate at run time
Visual Studio 2008 EE