# C++ 1D Array Question



## dcf-joe (Jul 10, 2009)

Why won't this work? My compiler tells me the only errors are located where the bold is.


#include <iostream>
using namespace std;

int main()
{
	int t, s, guess;
	cout << "Enter in the amount of storage spaces you need. ";
*cin >> s;
	int nums;*
	for (t = 0; t < s; t++)
	{
		cin >> guess;
		nums[t] = guess;
		"\n";
	}
	for (t = 0; t < s; t++) cout << "\tYou entered " << nums[t];
	return 0;	
}


----------



## FordGT90Concept (Jul 10, 2009)

Off the top of my head, I don't see anything wrong.  What is the error message?  Is it a run time error or compile error?


----------



## dcf-joe (Jul 10, 2009)

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'nums' : unknown size


----------



## FordGT90Concept (Jul 10, 2009)

Ah, "expected constant expression."  You can't define the array size at run time, it has to be defined at compile time.  That is, it needs to be constant.

For instance...


```
#include <iostream>
using namespace std;

int main()
{
int t, s, guess;
cout << "Enter in the amount of storage spaces you need. ";
cin >> s;
int nums[100];
for (t = 0; t < s; t++)
{
cin >> guess;
nums[t] = guess;
"\n";
}
for (t = 0; t < s; t++) cout << "\tYou entered " << nums[t];
return 0; 
}
```
s tells you how many are actually there.  In the example above, I fixed the size to 100 which is the maximum number it will fit.


You could also change it to a vector.  There's an example here:
http://www.codeguru.com/forum/archive/index.php/t-339603.html


----------



## temp02 (Jul 10, 2009)

As *FordGT90Concept* said, you can define a maximum value for the array size, if you are sure that that value won't be reached, but you may want to try replacing this:

```
int nums[s];
```
with:

```
int* nums = malloc(s * sizeof(int));
```

*Explanation:* in C (and C++) all arrays are pointers to a continuous area in the memory were values are stored, so instead of creating this memory area at compile time you should create it at runtime (since it has a variable size). To do this you need to create a pointer (signaled by *) and on this example a pointer to an integer (so "int *"). Then you need to Allocate Memory (malloc), to assing to the pointer, with the size of the data you want, wich you think that should be _s_, but the array size is measured in bytes and not in items, so we need _s_ times the size of one integer (measured at compile time by "sizeof(int)"; on an a "regular" x86 machine this number will be 4 (each integer is made-up/ocuppies 4 bytes on RAM)). To finish and since pointers are arrays and vice-versa, we will use "nums" to access our array without needing to change anything.

This might sound a bit technical but it's the right approach, you should read a bit about arrays and it's relation to pointers (if this is an class example don't worry you will). I tried to explain it the best way I can without talking about many other stuff, hope you understand.


----------



## dcf-joe (Jul 10, 2009)

I finally got it. I used this line of code in bold.

#include <iostream>
using namespace std;

int main()
{
	int t, s, guess;
	cout << "Enter in the amount of storage spaces you need. ";
	cin >> s;
*int *nums = new int;*
	for (t = 0; t < s; t++)
	{
		cin >> guess;
		nums[t] = guess;
		"\n";
	}
	for (t = 0; t < s; t++) cout << "\nYou entered " << nums[t];
	cin >> s;
	return 0;
}


----------

