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!