# Problem with assigning value to array elements

Printable View

• 08-17-2004
sagitt13
Problem with assigning value to array elements
Hi, I'm new to this site and programming as a whole, but since I'll be starting college this fall as comp sci major I thought I'll start to learn c++ by myself. I have MS Visual C++ 6 and I was looking through the tutorial included with it. I got to arrays and thought I'll try writing a very simple program with some stuff I've learned, but I ran into a problem. Here's the code.
Code:

```//Finding exponents of 2 with array #include <iostream> #include <iomanip> using namespace std; int main() {                int exponent;                                                //declare int variable exponent         cout << "What would you like to raise 2 to? \n";        //ask for user input                cin >> exponent;                                        //user input 'exponent'         int power[exponent+1];                                //declare array 'power' with 'exponent+1' elements                                                                                      //so index will be from 0 to 'exponent number'         power[0] = 1;                                                //assign power[0] as 1 since 2^0 = 1                                                                                        int i = 1;                                                                //i is counter                 do                                                                        //loop to assign values to array power         {                 power[i] = power[i-1]*2;                //each element value in array is 2 times the value before it                 ++i;                                                        //increment counter by one         } while (i <= exponent);                        //repeats until i reaches exponent                                for(i=0; i<=exponent; i++)                        //loop to display each value in the array         {                 cout << "2 to the " << i;                 if (i=1)                         cout << "st";                 else if (i=2)                         cout << "nd";                 else if (i=3)                         cout << "rd";                 else                         cout << "th";                 cout << " power is " << power[i] << ". \n";         }         return 0; }```
These are the error messages when I try to compile.

D:\Program Files\Microsoft Visual Studio\MyProjects\learn\learn.cpp(12) : error C2057: expected constant expression
D:\Program Files\Microsoft Visual Studio\MyProjects\learn\learn.cpp(12) : error C2466: cannot allocate an array of constant size 0
D:\Program Files\Microsoft Visual Studio\MyProjects\learn\learn.cpp(12) : error C2133: 'power' : unknown size
Error executing cl.exe.

I assume the errors say the array has unspecified size, but wouldn't [exponent+1] specify it according to user input?
Please explain what the problem is, sorry if my code looks bad, but I've only just started learning >_<.
Thanks!
• 08-17-2004
jverkoey
Welcome to the boards! Thanks for using code tags too, makes everyone here have a little more faith in new people when they use code tags right away.:D

With C/C++, you can't declare arrays with a variable size, unless you use dynamic memory.

For example:

Say you want to make an array of 10 ints, and you always know it's going to be 10, so logically you'd do:
Code:

`  int array[10];`
Well, what if you don't know what size it's going to be, like in your case where you have a variable? Well, because of the way the compiler works, if you don't tell it how big to make the variable it complains about creating it and will generate errors, so this is where dynamic memory comes in:

Take this example in to account, following much like yours:
You get a user's input and ask them how many ID numbers they want to input, and you want to grab each one and put it in an array of ints, so logically, the first step you'd take would be to do:
Code:

```  int size;   cout << "Please enter the number of pin numbers: ";   cin >> size;   int array[size];```
But of course the compiler complains, as you've already seen. So what you do is use the "new" operator, which I'm assuming you haven't seen yet, so here's a quick example with it:
Code:

```  int size;   cout << "Please enter the number of pin numbers: ";   cin >> size;   int *array=new int[size];   if(array)        // Make sure it allocated memory   {   // Do stuff     // Then, when you're done, you have to clean up after yourself             delete [] array;   }```
I'm not sure if you've worked with pointers yet or not...but basically what that does is asks for an array of <size> ints. So if we input 10 for the number of ids, it would create an array of 10 ints, dynamically.

Also, because we're doing this dynamically, we have to clean up after ourself so you don't get what's called a memory leak, where you start basically eating up ram and not giving it back.

Here's some links from the faq:
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
they basically just reiterate what I just said

-Hope that helps
• 08-17-2004
The Brain
:)
i just want to say to sagitt13 that your code looks really good.. the formatting is superb and your code is well commented. you are on your way to success. :)
• 08-17-2004
sagitt13
Thanks people! You're right, I haven't learned new operator or dynamic memory or whatever (they come after the current chapter in my tutorial). I guess I should read them before I try making my own code... :D Anyway thanks.