OK. I thought I understood pointers and such but this exercise is revealing that I do not. The task is to read characters into an array created on free store memory (heap), then print them out using the reference and dereference operators.
My code (below) compiles and runs but does not deliver the correct output. The value pointed to is special character like a playing card suit or a brace bracket. The address is the same for all the characters. The variable is not incrementing or it is always pointing to the first element of the array. I need help figuring out what is going on. Here is my code:
#include "../../std_lib_facilities.h"
int main ()
{
char* ch = new char[10];
char c = ' ';
cout << "Enter characters separated by a space.\nUse the '!' to indicate the end of your entries." << endl;
while (cin >> c) {
if (c == '!') {
break;
} else {
*ch = c;
ch++;
}
}
for (int i = 0; i < 10; i++) {
cout << *ch << " " << &ch << endl;
ch++;
}
delete[] ch;
keep_window_open();
return 0;
}