INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

HANDLE


PASSWORD
Remember Me
Forgot Password?

Come Join Us!

  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • Turn Off Ad Banners
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

E-mail*
Handle

Password
Verify P'word
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Member Feedback

"...Keep up the good work - excellent site - i'd been looking for something like this for ages !..."

Geography

Where in the world do Tek-Tips members come from?

Microsoft: Visual C++ FAQ

VC++ Problem

Why does my std::getline() read blanks? Why do I need to press the enter key twice whenever I use std::getline() in VC++? How do I fix those problems?
Posted: 10 Jun 04

Q: Why does my std::getline() read blanks?

A: Actually, your std::getline() does not read blanks.  Instead, it reads the leftover delimiter (such as '\n' or ' ') from the previous input stream operation.  Usually it happens when you use "cin>>buffer" followed by a call to "getline()".

Consider the following stream of data:

"I am so confused."

CODE

cin>>buffer_1;
getline(cin,buffer_2);

cin will read 'I' but it does not read the white space ' ' following 'I'.  Thus, buffer_1 will store 'I' and buffer_2 will get the white space ' ' between 'I' and am.  That's why you get blanks for your buffer_2.  

Solution:
Use cin.ignore() to ignore the unwanted delimiter.  

CODE

cin>>buffer_1; //buffer_1="I"
cin.ignore(1,' ');
getline(cin,buffer);//buffer_2="am so confused."

Q: Why do I need to press the enter key twice whenever I use std::getline() in VC++?

A: There is a bug in VC++'s implementation of string.  This has been posted for quite sometimes by Microsoft at http://support.microsoft.com/default.aspx?scid=kb;en-us;240015 Thus, VC++'s std::getline() will read an extra character after encountering a delimiter.

Solution:
Go to your include folder (typically it can be found at C:\Program Files\Microsoft Visual Studio\VC98\Include) and edit STRING.  NOTE that the file name is STRING and not STRING.H.  Make the changes as mentioned in the article.

Back to Microsoft: Visual C++ FAQ Index
Back to Microsoft: Visual C++ Forum
My FAQ Archive
Email This FAQ To A Friend

My Archive