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.txtWhat do you want write in your file?This is a good exampel
#include <iostream>
#include <fstream>
usingnamespace 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)