# Maximum PC

 It is currently Sat Apr 19, 2014 8:48 am

 All times are UTC - 8 hours

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: C++ Array helpPosted: Sun Oct 18, 2009 9:31 am
 Team Member Top 1000

Joined: Wed Jul 02, 2008 1:41 pm
Posts: 252
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

Top

 Post subject: Posted: Mon Oct 19, 2009 4:20 am
 SON OF A GUN

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
You can't assign a dynamic size to the array. It has to be constant.

If you truly need a dynamically sized array, look into using vectors.

Top

 Post subject: Posted: Wed Oct 21, 2009 9:53 pm
 8086

Joined: Fri Jan 11, 2008 7:40 pm
Posts: 19
Also, remove the "const" modifier for the declaration of inputLength. You are trying to assign a dyanmic value to what should be a static variable. Use const only for known and unchanging values. For example, let's say you are creating a program/methods that calculate different measurements of a circle like area and circumference. Both use the value of PI and that value is always the same. Then you would declare the variable PI as a constant equaling 3.14....

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 3 posts ]

 All times are UTC - 8 hours

#### Who is online

Users browsing this forum: No registered users and 3 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ Maximum FAQs    Forum Rules, Posting Guidelines & Announcements    The Good, The Bad & The Banned    FAQs Help/Do It Yourself    PC Building Lab    The Help Desk    PC Modding    Education & Certification Hardware    Nuts & Bolts    Portable Talk    Appraisals, Deals & Bargains (oh my!) OS/Software/Programming    Windows Parlor    Alt.OS.Abode    Games Arena    Programmers' Paradise Networking/Internet    Internet Truckstop    Network Nook In/Out    Magazine and Book Feedback    Forum & Website Feedback    Dog Pound Team Maximum PC Folding at Home    Team Maximum PC - Folding at Home - FIND CURES TO DISEASES    Team MPC - Folding Gauntlets