# C++ getline(cin, mystr) doesn't work on windows



## wookie2u (May 6, 2007)

Folks,

I'm a C++ newbie, though I know some ANSI C. I'm working through the tutorials at http://www.cplusplus.com/doc/tutorial/ (which I highly recommend, so far).

I'm having some fun with the *getline*  function.

Here's the problematic code:

```
// cin with strings
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string mystr;

  cout << "What's your name? ";
  getline(cin, mystr);

  cout << "Hello " << mystr << ".\n";
  cout << "What is your favorite team? ";
  getline(cin, mystr);

  cout << "I like " << mystr << " too!\n";

  return 0;
}
```
The *getline()* function does not work as expected... 
1. The first getline blocks until I press return *twice*, and (correctly) stores the input in mystr. 
2. The second call to _getline_ (correctly) blocks until I press return once, but doesn't store anything in mystr.

Execution looks like this:

```
C:\projects\vcpp\tutorials\cplusplus_com\cplusplus_com>GetLine
What's your name? Keith

Hello Keith.
What is your favorite team? Your Momma
I like  too!

C:\projects\vcpp\tutorials\cplusplus_com\cplusplus_com>
```
I'm using the vc.exe compiler from "Visual C++ 2005 Express" in the "Visual Studio 2005 Command Prompt" (not just cmd) on Windows XP.

I suspect this has something to do with EOL sequence handling. Google found a number of similar posts, but nothing which looks (to ignorant me) "right on the money".

I humbly thank you for any enlightenment you might offer.... 

Keith.


----------



## wookie2u (May 6, 2007)

I got it.

I still don't know exactly why, but when I build the same code in the IDE "Visual C++ 2005 Express" it executes correctly in both the standard cmd and the special "Visual C++ 2005 Express Command Prompt".

I presume there must be some wacky flag (like charset or something) which I need on my command line builds. Guess I'm stuck using the IDE, which I hate passionately. 

Thanx anyway guys.

Keith.


----------

