Basically, the difference between
get and
getline when you're reading C strings is that
get leaves the delimiting character (a newline character, in this case) in the input buffer as the next character to be read, while
getline actually extracts (and discards) the delimiting character.
So the reason the code doesn't work when you use
get is that after the first time you call
cin.get(temp, 79);
, it leaves the delimiting newline character in the input buffer.
Then, the second time you call it,
cin immediately runs into the delimiting character (which is still in the input buffer) and thinks that the input stops right there, so it doesn't read any characters.
It works with
getline since
getline gets rid of that delimiter after it reads the input.
Just for fun, try using
get, but stick a
cin.ignore();
call after it:
27 28 29 30
|
cin.get(temp, 79);
cin.ignore(); // Discard one character from the input buffer (the newline)
char* pn = new char[strlen(temp) + 1];
// ...
|
800!