VC6 bug? cout/cin were not synchronized

The following code does not work correctly in VC6 (Visual Studio):

#include <string>
#include <iostream>
using namespace std;

int main()
{
    string s1, s2, s3, s4, s5;
    while (true)
    {
        cout << "Enter s1: ";
        getline(cin, s1);
        cout << "s1= " << s1 << endl;
        cout << "Enter s2: ";
        getline(cin, s2);
        cout << "s2= " << s2 << endl;
        cout << "Enter s3: ";
        getline(cin, s3);
        cout << "s3= " << s3 << endl;
        cout << "Enter s4: ";
        getline(cin, s4);
        cout << "s4= " << s4 << endl;
        cout << "Enter s5: ";
        getline(cin, s5);
        cout << "s5= " << s5 << endl;

        if (s5=="q")
            break;
    }
    return 0;
}

Running the prog in debugger gives that output:

Enter s1: a

s1= a
Enter s2: b
s2=
Enter s3: c
s3= b
Enter s4:
s4= c
Enter s5: d
s5=
Enter s1: a
s1= d
Enter s2: _

I needed to press ENTER twice at the first prompt. But obviously, the second linefeed closed getline(cin, s2) and the input entered at the "Enter s2: " prompt was read into s3 variable, and so on ...

Some months ago, an expert gave a bugfix for that bug (or a similar one), but I couldn't find that comment til now.  

Does anyone know that bugfix? Or knows a workaround?

----------------------------------------------------------------------------------------------------------------
I replaced getline(cin, si) by cin>>si and it works. However, when I entered two words separated by a space, only the first word was moved to the string variable and the second word became input for the next read.

Enter s1: a
s1= a
Enter s2: b
s2= b
Enter s3: c
s3= c
Enter s4: d e               <=== HERE I ENTERED A SPACE
s4= d
Enter s5: s5= e
Enter s1:

I know, that the reason for that behaviour is that iostream::operator>> takes space as a delimiter. But I would assume that delimiters could be changed.

Does anybody know how to change default delimiter to '\n'?

Regards, Alex



LVL 39
itsmeandnobodyelseAsked:
Who is Participating?
 
AxterCommented:
Hi itsmeandnobodyelse,

see http://support.microsoft.com/support/kb/articles/Q240/0/15.ASP ('BUG: getline Template Function Reads Extra Character'). There's also a fix provided with that article.

David Maisonave :-)
Cheers!
0
 
AxterCommented:
The suggested "workaround" involves modifying one of the STL header files.
0
 
itsmeandnobodyelseAuthor Commented:
Yes, it worked. I had fixed it in my home system but forgot the change ;-)

Does anybody know an answer to the second question about changing the default delimiter used by operator>>(iostream&, const string&) ?

Regards, Alex

0
 
AxterCommented:
>>Does anybody know an answer to the second question about changing the default delimiter used by operator>>
>>(iostream&, const string&) ?

You can change the default delimiter when you call the getline function.

getline(cin, s1, ' ');
getline(cin, s1, '\n');
0
 
itsmeandnobodyelseAuthor Commented:
Axter, I'll accept the answers, though I don't need to pass a delimiter to getline as the default already does what I need ;-)

If anybody knows how to solve the problem by using operator>>, I will open a new question and accept the answer there.

Regards, Alex
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.