Quantcast

Maximum PC

It is currently Tue Jul 29, 2014 5:52 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: C++ Array help
PostPosted: Sun Oct 18, 2009 9:31 am 
Team Member Top 1000
Team Member Top 1000
User avatar

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
  Profile  
 
 Post subject:
PostPosted: Mon Oct 19, 2009 4:20 am 
SON OF A GUN
SON OF A GUN
User avatar

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
  Profile  
 
 Post subject:
PostPosted: Wed Oct 21, 2009 9:53 pm 
8086
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
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


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

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group