[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 731
  • Last Modified:

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")
    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

  • 3
  • 2
1 Solution
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 :-)
The suggested "workaround" involves modifying one of the STL header files.
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

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now