Array output

Hello guys. I have a problem on this little program. Basically, I want to read an array and invert its the elements so if the input is [a; b; c] the output will be [c; b; a]. Now, I can't achieve the output. When I execute it on console, the output string just prints blank spaces. What am I doing wrong?

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using namespace std;


int main () {

	const int MAX = 20;
	unsigned int n;
	char array[MAX];
	char box;

	cout << "Insert the dimension of the array [1 - " << MAX << "]: ";
	cin >> n;
	while (n < 1 || n > MAX)
	{
		cout << "Error - the dimension must be included between 1 and " << MAX << ": ";
		cin >> n;
	}

	for (int i = 0; i < n; ++i) 
	{
		cout << "Insert the " << i + 1 << " element (a - z): ";
		cin >> array[n];
		while (array[n] < 'a' || array[n] > 'z')
		{
			cout << "Entry not valid! Insert again: ";
			cin >> array[n];
		}
	}

	for(int x = 0, y = n - 1; x < n / 2; ++x, --y)
	{ 
		box = array[y];
		array[y] = array[x];
		array[x] = box;
	}

	cout << "\nA = {";
	for (int k = 0; k < n; ++k)
	{              
		cout << array[k] << "; ";
	}
	cout << "\b\b}" << endl;

	return 0;
}
Look at lines 23, 24, and 27. Which array element are you operating on?


Hint in "debugging": you could print the array both before and after the inversion.


Note: You don't need both x and y on line 31. You can calculate y from x within the loop's body.
Line 20-29, should use array[i] rather than array[n].
This is what I found so far.
Thanks a lot guys!
Topic archived. No new replies allowed.