?
Solved

GetLine() Question

Posted on 2004-09-18
9
Medium Priority
?
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Accepted Solution

by:
efn earned 300 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 200 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

764 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