inputing elements into an array

hey guys, I'm playing around with a simple array with a for loop and I'm trying to make it possible for the user to fill the array up with what ever information he/she wants. how do I make it so a word can to be stored as a value to the element or if I type in 33 to the first element it will come out as 33 and not some weird number I didn't even select.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void arrayLoop()//function and name
{
	int i;
	//initialize array with amount of elements
	int seatNum[9];
	cout<<"Element  -  Value"<<endl;//prompt
	//loop (initialized; stopping point; increment amount)
	for(int x = 0; x<=8; x++){
		
		seatNum[x] = i;//initialize loop to array then attach a value
		cout<<"please fill the array "<<endl;//prompt
		cin>>i;
		cout<< x <<"   -------   "<< seatNum[x] << endl;

	
	}
}
I think you must move:
seatNum[x] = i;
to line after cin>>i;
Hi. I noticed that your array was declared as an integer array (which only takes numbers) and that would explain those weird numbers (memory addresses) you are getting when the user inputs letters.

One solution is to make a string array to allow for any input. I did make some small alterations for a few reasons, mostly out of habit and legibility, but I hope it was what you were looking to achieve. Just be sure to #include<string>.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void arrayLoop()
{
	const int arraySize = 8;	
	string seatNum[arraySize]; 
	int i;
	
	for (i = 0; i < arraySize; i++)
	{
		cout << "Please fill the array: ";
		cin >> seatNum[i];	
	}

	for (i = 0; i < arraySize; i++)
		cout << "\nElement " << i << " is: " << seatNum[i];
		cout << endl;
}
@FvPJer

I noticed that your array was declared as an integer array (which only takes numbers) and that would explain those weird numbers (memory addresses) you are getting when the user inputs letters.


What was displayed is not a memory address. It is the max number an int can hold.
Topic archived. No new replies allowed.