|
From: Nicholas O. R. <exc...@mi...> - 2004-05-21 23:19:56
|
Before each call to cin.getline(), add this line:
cin.ignore();
The reason is this: When you use "cin>>" to take in data, it reads
everything except the newline ('\n') character, which it leaves on the
input buffer. When you use "cin.getline()" to take in data, it reads
everything including the newline character, which it throws away. Each
time you have a call to "cin.getline()" after a call to "cin>>", the
getline function picks up the newline left on the buffer by "cin>>" and
interprets that as the end of the input, thus exiting the getline
function. By putting a "cin.ignore()" between the "cin>>" and the
"cin.getline()", you remove the pesky leftover newline character from
the buffer. It is important to note that if you use "cin.ignore()"
before a getline when there is no leftover newline character sitting on
the buffer, the next character you type in will be taken in by the
"cin.ignore()" and not the "cin.getline()"... i.e. it will look like the
first character of your input got 'eaten'.
Nicholas
|