getline(cin,string) doesn't work for files?

I have got a problem with the getline(cin,string) command. When I am trying to store a full text in a new file, it doesn't work.
Therefore I am forced to use the cin>>string... E.g. if I write:
What do you want to call your file?
Exampel.txt
What do you want write in your file?
This is a good exampel

When I open the file I only get the first word!!!
This


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <fstream>
using namespace std;

string a;
string b;
string output;

int main () {
    cout<<"What do you want to call your file?"<<endl;
    cin>>a;
    ofstream myfile;
    myfile.open (a.c_str());
    
cout<<"What do you want write in your file?"<<endl;
    //getline(cin,b) doesn't work???
    cin>>b; //This should instead be getline(cin,b) !!!
    myfile <<b;
    myfile.close();

ifstream myReadFile;
    myReadFile.open(a.c_str());
    
    myReadFile >> output;
    cout<<output<<endl;

    myReadFile.close();
}


Anybody who can help me? And what's the problem with getline(cin,string)?
cin>>b; //This should instead be getline(cin,b) !!!  
This is a formatted input function, so it stops on first whitespace found. Working as intended.

//getline(cin,b) doesn't work???  
It is actually works exactly as documented. Classic problem of "getline after formatted input": http://stackoverflow.com/a/21567292
In short: std::getline(std::cin >> std::ws, b)
Topic archived. No new replies allowed.