Because there was an error in the code when I posted this question, it is not a good question. I have deleted and replaced it with a link to a correct solution.
cin.getline(buffer, '\n'); <-- is wrong, need buffer size.
cin.getline(buffer, 10000, '\n');