Solved

GetLine() Question

Posted on 2004-09-18
9
247 Views
Last Modified: 2012-05-05
I am using get line to store a string:

this is an example program:


#include <iostream>
#include <string>
#include <iomanip>

int main()
{
string strName;

cout << "Enter Name: ";
getline (cin, strName);

return 0;
}

the problem is that when I use get line, you have to hit enter twice. What will happen is, I will hit enter it will move to the next line, but I have to hit enter again for input to terminate.


Why is it doing this?

-Brian
0
Comment
Question by:BrianGEFF719
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 23

Expert Comment

by:Mysidia
ID: 12094292
That's rather unusual.. which compiler version/OS environment?
0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 12094434
Visual C++ 6.0
0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 12094436
VC++ 6, Windows XP SP2
0
 
LVL 15

Accepted Solution

by:
efn earned 75 total points
ID: 12094454
This is a known bug in this compiler's library.

http://support.microsoft.com/support/kb/articles/Q240/0/15.ASP

http://www.dinkumware.com/vc_fixes.html

It's also documented in a number of PAQs here on Experts Exchange.

--efn
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12094462
On a WinXP VC 6.0 environment happens the same. It is because of that sequence in <string> include file:

        for (; ; _C = _I.rdbuf()->snextc())
            if (_Tr::eq_int_type(_Tr::eof(), _C))
                {_St |= ios_base::eofbit;
                break; }
            else if (_Tr::eq((_E)_C, _D))
                {_Chg = true;
                _I.rdbuf()->snextc();
                break; }
            else if (_X.max_size() <= _X.size())
                {_St |= ios_base::failbit;
                break; }
            else
                _X += _Tr::to_char_type(_C), _Chg = true;

The first 'else if'  truely finds out that the terminating character was entered *but* calls again _I.rdbuf()->snextc(); So you have to hit <Enter> again. I don't know if that is a bug or how to overcome that strange behavior.

Regards, Alex
 
0
 
LVL 23

Assisted Solution

by:Mysidia
Mysidia earned 50 total points
ID: 12094468
It seems to be one of the bugs in the C++ library included with MSVC

See this page:
  http://people.msoe.edu/~taylor/resources/msvc60.htm

the subitem called getline()
apparently you can patch the header files to fix this problem
(be sure to back up any compiler header files to be changed before editing them)
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12094470
@efn

thanks, for the answer. I am going to modify STL header file. Or should i rewrite std::string? ;-)

Regards, Alex
0
 
LVL 3

Expert Comment

by:teratoma
ID: 12094711
Have you tried cin.getline?
0
 
LVL 23

Expert Comment

by:Mysidia
ID: 12097596
If you want to resort to changing your own code, then you could try the simpler C stdio library
if you like...

#include <iostream>
#include <string>
#include <iomanip>
#include <cstdio>

int main()
{
  string strName;
  char buf[512];

   printf ("Enter name:");

   if ( fgets(buf, sizeof(buf), stdin) ) {
      strName = string(buf);
   } else {
      fprintf(stderr, "Error: unable to read name\n");
      return 1;
  }

  return 0;
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

930 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

12 Experts available now in Live!

Get 1:1 Help Now