Array

My aim is to allow the user to input positive integers, which will be stored in an array with just enough memory, so each time a new number is entered, the array size is increased by 1. When -1 is entered, the loop is stopped and the entered numbers are printed out.

I am getting 3 errors, expected constant expression, cannot allocate an array of constant size 0 and numArray unknown size. Could someone please help, its a simple issue I just cant get my head around it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;


int main()
{
	int num = 0;
	int ARRAY_SIZE = 1;

	int numArray[ARRAY_SIZE];


	while ( num >= 0){
		ARRAY_SIZE = ARRAY_SIZE + 1;
		int i=0;
		cout << "Please enter num" << endl;
		cin >> num;
		numArray[i] = num;
		i++;
		ARRAY_SIZE = ARRAY_SIZE + 1;
	}

	for( int i = 0; i<5; i++){
		cout << numArray[i] << endl;
	}

}


In your code, I can see your increasing the size of your array as you go. You can only allocate a set amount of space for an array.
If you initialize your array like that (int numArray[ARRAY_SIZE]), it's good to initialize ARRAY_SIZE as a const int ARRAY_SIZE = 15, where const int means that it will never change.
I would use a for() loop to get the input (int i, i < ARRAY_SIZE; i++), then as you did, a for() loop to print out the input.
Last edited on
If you want to resize array you should use dynamic arrays but increasing it's size means to copy all of it's elements to a new array
(if you are interested in doing so see http://www.cplusplus.com/doc/tutorial/dynamic/ )
If you want to do it the best way, use a std::deque ( http://www.cplusplus.com/reference/stl/deque/ ) and use push_back
Arrays don't work that way; they have constant size.

That is, once the compiler sees line 10, you have an array of one element. What you do to ARRAY_SIZE after that doesn't affect the array itself.

By the way, declaring an array as you do on line 10 is not ISO C++ compliant -- some compilers let you get away with it but it is still not legal.

There are three solutions:

Solution One
Just allocate a really big array and barf if the user enters too many elements:
1
2
3
4
5
int main()
{
	const int ARRAY_CAPACITY = 1000;
	int numArray[ ARRAY_CAPACITY ];
	int array_used = 0;
Every time you bump array_used you just need to check that it is still smaller than ARRAY_CAPACITY.


Solution Two
Use dynamic memory:
1
2
3
4
5
6
7
int main()
{
	int* numArray = NULL;
	int array_size = 0;
	...
		array_size++;
		numArray = (int*)realloc( numArray, sizeof(int) * array_size );


Solution Three
Use a C++ container:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <vector>
using namespace std;

int main()
{
	int num = 0;
	vector <int> numArray;

	while (num >= 0) {
		...
		numArray.push_back( num );
	}

	for (unsigned n = 0; n < numArray.size(); ++n) {
		cout << numArray[ n ] << endl;
	}

	return 0;
}


Hope this helps.
Topic archived. No new replies allowed.