|
-
Feb 20th, 2002, 11:37 AM
#1
getline and cin
I have a problem using cin and getline:
Code:
string str;
getline(cin, str);
This requires the user to hit enter twice. It seems that there is some buffer storing one string and getline retrieves only what is pushed out of the buffer. This is because if I do a
cin.putback('\n');
first, the user needs to hit enter only once after his input, but the retrieved string is only the \n I pushed to the input buffer. Likewise, when I do
Code:
string str;
cout << "Enter a string:" << endl;
getline(cin, str);
cout << str << endl;
cout << "Enter another string:" << endl;
getline(cin, str);
cout << str << endl;
the program first asks for a string. Then it waits two times for input ( the user will probably just hit enter ), then output the first line, then ask for another string. While this time the user needs only input one string, the program will then output not this string, but rather the second one from the first getline call.
Any ideas how to solve this?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|