[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

GetLine() Question

Posted on 2004-09-18
9
Medium Priority
?
257 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

656 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