Writing a string with paragraphs to a file

Hello, I'm trying to let the user enter a string that will later be written to a file. The problem is that I don't know how to let the user enter paragraphs. When the user presses Enter, the file is written and the program ends.

I have tried to include a while statement, but I don't seem to get it right and so I have not posted it.

Can you help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <fstream>
#include <iostream>
#include <string>

int main()
{
    using namespace std;

    cout << "Write something to a file: " << endl;
    char str[1000];
    cin.getline( str, 1000 );

    ofstream outf("C:\sample.txt", ios::app);

    if (!outf) {
        cerr << "The file could not be opened for writing!" << endl;
        exit(1);
    }

    outf << str << endl;
	return 0;

}
You need to put your getline inside a while loop that tests a bool variable before every interation. Make an uncommon command exit the process something like "!!GOBLINSROCK!!" so that your loop tests the users data everytime it executes and sets the bool variable to false when it sees this entry causing the loop to exit.
Press ENTER twice to finish:
http://www.cplusplus.com/forum/beginner/2409/#msg9254

(Instead of using a stringstream to convert to pairs, just store the line.)
Topic archived. No new replies allowed.