char pointers are not strings. They're pointers.
arr[i] = input;
Here, arr[i] (that is, all 5 elements of your arr array) will point to the same string that input points to.
Therefore when you print them:
1 2
|
for(int i = 0; i < 5; ++i)
cout << arr[i];
|
This is like printing 'input' 5 times in a row. Each time it will print the same thing.
EDIT:
To clarify, you only have 1 string here: 'input'. Your arr array basically just acts as an alias to access input via different pointers. Each time you get a new string from the user, you're overwriting your previous string.
Of course... this code is
very bad if input is also just a pointer and not an actual buffer. In which case you are corrupting the heap. How is input declared?
EDIT 2:
The easy solution is to forget about char arrays and just use strings:
1 2 3 4 5 6 7 8 9 10
|
// make sure 'input' is also a string
string arr[5]; // make these strings, not pointers...
int i = 0;
while (i++ < 5) {
cout << "type " << i << ". string: ";
cin >> input;
arr[i] = input; // now this will work the way you want
}
for(int i = 0; i < 5; ++i)
cout << arr[i];
|