User setting arrayvalues in a for-loop

Hi,
how can I create a for-loop that lets the user enter values into an array?

cheers!
i don't know exactly what you want, but let's say it's something like this:

1
2
3
4
5
6
7
8
9
#include <iostream>

int main(){
  int array[10];
  for (int x=0; x<10; x++){
    std::cin >> array[x];
  }
  return 0;
}


this way, you create an array that contains 10 elements. the for-loop creates the int x, and increment it by 1. so, it will be run ten times, ranging from 0 to 9 (arrays always start counting from 0). every time, it asks for an user input.
note that this code won't have any output.

is that what you want?
You can do it using the variable you create in the for loops initialization expression as the element number of the array you want to input to. Something like this:

1
2
3
4
for (int i = 0; i < 10; i++)
{
    cin >> array[i];
}


This is assuming you declared the array as array[10]. If you have the user enter the number of inputs they wish to make you can replace 10 with the variable name that holds the size they enter.
Thanks but It dont seem to work? Cant figure whats wrong?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
	int x [5];
	const int size = 5;
	for(int i = 0; i < size ; i++)
	{
		cout << "Set value to 'i': " << endl;
		std::cin >> x [i];
	}

	for(int i = 0; i < size; i++ )
	{
	cout << "array 'i' contains: " << x << endl;
	}
}



and the console displays:

Set value to 'i':
1
Set value to 'i':
23
Set value to 'i':
54
Set value to 'i':
13
Set value to 'i':
453
array 'i' contains: 0xbf88a58c
array 'i' contains: 0xbf88a58c
array 'i' contains: 0xbf88a58c
array 'i' contains: 0xbf88a58c
array 'i' contains: 0xbf88a58c



Cant really figure whats going on? Seem to be that it only display the arrayadress and if so, what have I done wrong?
Secondly, do I write to five diffrent slots in the array with this code or do they all just get overwritten by each other in the first memoryslot [0] ?
Last edited on
When you declare an array what it actually is is a set of data stored in sequential memory blocks. The array name itself (in your case x) is actually the address of the first element. That's why you are outputting the address on your last 5 lines. To output the values in each element of the array you need to use the same form you did to input it, ie use x[i] instead of x in your output for loop. This will output the value contained in each element.
When you declare an array what it actually is is a set of data stored in sequential memory blocks. The array name itself (in your case x) is actually the address of the first element. That's why you are outputting the address on your last 5 lines. To output the values in each element of the array you need to use the same form you did to input it, ie use x[i] instead of x in your output for loop. This will output the value contained in each element.


Oh I see, thank you so much =)
Topic archived. No new replies allowed.