VC6 bug? cout/cin were not synchronized

Posted on 2005-04-21
Last Modified: 2008-03-10
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")
    return 0;

Running the prog in debugger gives that output:

Enter s1: a

s1= a
Enter s2: b
Enter s3: c
s3= b
Enter s4:
s4= c
Enter s5: d
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

Question by:itsmeandnobodyelse
    LVL 30

    Accepted Solution

    Hi itsmeandnobodyelse,

    see ('BUG: getline Template Function Reads Extra Character'). There's also a fix provided with that article.

    David Maisonave :-)
    LVL 30

    Expert Comment

    The suggested "workaround" involves modifying one of the STL header files.
    LVL 39

    Author Comment

    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

    LVL 30

    Expert Comment

    >>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');
    LVL 39

    Author Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now