Solved

GetLine() Question

Posted on 2004-09-18
9
249 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

856 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