-
Problem with arrays
Hi,
I'm trying to create an array with as many elements as the users inserts.
Code:
#include <iostream>
using namespace std;
int main() {
int number = 1;
cout << "Number of elements in array?\n";
cin >> number;
int elements[number];
cout << "Insert the elements\n";
for ( int a = 0; a <= number; a++ ) {
cin >> elements[a];
}
return 0;
}
My compiler (MS Visual C++ 6.0) keeps telling me, that "numbers" must be constant...Is there any way to pass that nad make my code work?
-
> int elements[number];
Well you could use std::vector
Or maybe
int *elements = new int[number];
But remember to do
delete [ ] elements;
when you're done.
> for ( int a = 0; a <= number; a++ )
Arrays start at 0, and end with the n-1 element.
So it's
for ( int a = 0; a < number; a++ )
-