Problem with arrays

This is a discussion on Problem with arrays within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to create an array with as many elements as the users inserts. Code: #include <iostream> using namespace ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    19

    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?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > 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++ )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    19
    Thanx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework problem...structures or arrays?
    By tortan in forum C++ Programming
    Replies: 21
    Last Post: 08-30-2006, 01:26 AM
  2. Help with a problem regarding dynamic memory and arrays
    By Michty in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2006, 01:26 PM
  3. assignment of arrays problem
    By HumbuckeR in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2006, 04:25 PM
  4. Problem with character arrays in classes
    By spoketoosoon in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2004, 02:57 AM
  5. Replies: 5
    Last Post: 12-03-2003, 04:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21