Solved

GetLine() Question

Posted on 2004-09-18
9
246 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

708 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

13 Experts available now in Live!

Get 1:1 Help Now