Need help with Arrays

For example, I entered :

Enter a number: 5

Enter element 0 values: 10
Enter element 1 values: 20
Enter element 2 values: 30
Enter element 3 values: 40
Enter element 4 values: 50
Enter element 5 values: 70

In the last part, I just want the Element values to appear like this.

Element values are: 10 20 30 40 50 70

but its showing this instead :

Element values are: 70 70 70 70 70 70

So the last value, I entered is the one that repeatedly appears in the "Element values"

So please help me with this code, I cant seem to find the solution.
Thanks !

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include<iostream>
using namespace std;
int main ()
{
	int arrsize = 0;
	cout<<"Enter a number: ";
	cin>>arrsize;
	int num[arrsize];
	for (int xox = 0; xox <= arrsize; xox++)
	{
		cout<<endl;
		cout<<"Enter element "<<xox<<" values: ";
		cin>>num[arrsize];
	}
		cout<<endl;
		cout<<"Element values are: ";
	for (int xox = 0; xox <= arrsize; xox++)
	{	
		cout<<num[arrsize]<<" ";
	}	
		

}


Line 8: This isn't standard C++ syntax, although some compiler support it. Array sizes must be known at compile time (when you compile the program). Ideally you should use a vector<int> instead, but for now, you might want to just get it working with the extension.

Line 9. When you create an array of 5 elements, the indexes are 0..4, not 0..5. So the test should be xox < arrsize

Line 13: You're storing into element number arrsize, which is actually out of bounds. So in your example, you store every number into num[5], overwriting the previous value each time. You need to store into num[xox].

Line 17: Same comment as line 9.

Line 19: Same problem as line 13: you're printing the same number each time.
Thanks for the help, it finally worked

I changed the :
Lines 9, 13, 17, & 19
Topic archived. No new replies allowed.