Thread: Problem with assigning value to array elements

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    7

    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!

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    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.

    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
    Last edited by jverkoey; 08-17-2004 at 02:00 AM.

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    :)

    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.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    7
    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... Anyway thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Left Shift elements of a 2D array
    By w2look in forum C Programming
    Replies: 8
    Last Post: 01-23-2009, 12:13 AM
  2. Small problem with printing elements of an array
    By DLR in forum C Programming
    Replies: 17
    Last Post: 03-09-2006, 06:57 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. array problem
    By correlcj in forum C++ Programming
    Replies: 5
    Last Post: 11-09-2002, 07:58 AM
  5. array shift problem
    By splendid bob in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2002, 10:11 PM