Question: I'm trying to make a program that will get the user input of a new file name, create the file, and write to it. It works but it will only write the first word of the string to the file. How can i get it to write the full string? thanks.

```
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
for (;;)
{
     char  *myFile = " "; 
     string f = " ";
     string w = " ";
     cout <<"What is the name of the file you would like to write to? " <<endl;
     cin >>f;
     ofstream myStream(f,ios_base::ate|ios_base::out);

     cout <<"What would you like to write to " <<f <<" ? ";
     cin >>w;

     myStream <<w;

  if (myStream.bad())
  {
     myStream <<"A serious error has occured.";
     myStream.close();
     break;
  }
}

}

```

Answer: 
According to [this post](https://stackoverflow.com/questions/5455802/how-to-read-a-complete-line-from-the-user-using-cin), you should consult [this reference](http://www.cplusplus.com/reference/fstream/fstream/) to use a method like getline().

Also, when you are writing out I recommend that you flush the output (cout.flush()) before ending the program, especially in this case, since I presume you are ending the program with a ctrl-C break.

In formulating a suggestion, I will read data into char\*, and convert them to "string" in case you will use them elsewhere in your program.

I tested this code in MS Visual C++ Express.

```
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
    for (;;)
    {
        char  *myFile = new char[200]; // modified this line

        //added this line
        char *myInput = new char[200];

        string f = " ";
        string w = " ";
        cout << "What is the name of the file you would like to write to? " << endl;
        cin.getline(myFile, 200);//modified this line
        f = (myFile);//added this line
        cin.clear(); //added this line
        ofstream myStream(f, ios_base::ate | ios_base::out);

        cout << "What would you like to write to " << f << " ? ";

        cin.getline(myInput, 200);//edited this line

        w = string(myInput);//added this line

        myStream << w;
        myStream.flush();//added this line

        if (myStream.bad())
        {
            myStream << "A serious error has occured.";
            myStream.close();
            break;
        }

        delete myFile;
        delete myInput;
    }

}

```
You have to use `std::getline()` to read the entire line:

```
std::getline(std::cin >> std::ws, w);

```

The `>> std::ws` part gets rid of leading whitespace which is needed because the newline left in the stream from the previous extraction will prevent `std::getline()` from fully consuming the input.

When inserting data into the stream you need to make sure it gets flushed (because, as the other answer said, you're probably using `Ctrl+C` to terminate the program and you may not see output during the program run). You can use the `std::flush` manipulator to flush the output:

```
myStream << w << std::flush;

```