getlines..

This is a discussion on getlines.. within the C++ Programming forums, part of the General Programming Boards category; hey whats up..i hate getlines..we're working in UNIX on a binary file program..
and i need to ask for a ...

getlines..

hey whats up..i hate getlines..we're working in UNIX on a binary file program..

and i need to ask for a persons name...and i tried to ways..

way 1)

Code:

string key;
cout << "Enter name\n"; //input: Michael Jordan
getline(cin, key, '\n');
cout << key << endl; //output: it skips over this, doesn't halt at the getline to get the value...rather just moves right along w/ the function and the key being some crazy value

way 2)

i just inserted a cin.ignore to see what would happen..and it halts, but still pulls out a " " instead of "Michael Jordan", does anyone know what i'm doing wrong?

Fix to <string>
The header <string> contains a definition for template function getline. It has a lookahead problem -- typing a delimiter to end the input doesn't return control until you type yet another character. Change the code as indicated by the comment: