User Information into a text file

Hi guys, I am trying to make a program write user entered text into a .txt file. With the program at the moment the .txt file will create but no text will be in the file. Just wondering why this is. The code for this is in a function.
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
29
30
31
32
33
34
35
36
37
	float write();
	{
		string firstline, secondline, thirdline, fourthline;
		

		system("cls");

		cout << "Write to a file" << endl << endl;
		cout << "Enter first line you wish to write to a file: " << endl;
		cin >> firstline;
		cout << endl << endl << "Enter second line you wish to write to a file: " << endl;
		cin >> secondline;
		cout << endl << endl << "Enter third line you wish to write to a file: " << endl;
		cin >> thirdline;
		cout << endl << endl << "Enter fourth line you wish to write to a file: " << endl;
		cin >> fourthline;

		string poem = "\n\t"; firstline;
		poem.append( "\n\t"); secondline;
		poem.append( "\n\t"); thirdline;
		poem.append( "\n\t"); fourthline;


		ofstream writer("poem.txt");

		if (! writer)
		{
			cout << "Error opening file for output" << endl;
			return -1; 
		}
		writer.open("poem.txt", ios::out);
		writer << poem << endl;
		writer.close();

		system("pause");

	}
18
19
20
21
string poem = "\n\t"; firstline;
poem.append( "\n\t"); secondline;
poem.append( "\n\t"); thirdline;
poem.append( "\n\t"); fourthline;


This won't work! The only thing you're appending to the poem variable is newlines and tab characters. See those semicolons in those lines? They separate statements, and usually statements are given their own lines. You're essentially saying:
18
19
20
21
22
23
24
25
string poem = "\n\t";
firstline;
poem.append( "\n\t");
secondline;
poem.append( "\n\t");
thirdline;
poem.append( "\n\t");
fourthline;

Try this:
18
19
20
21
22
23
24
25
string poem = "\n\t";
poem.append(firstline);
poem.append( "\n\t");
poem.append(secondline);
poem.append( "\n\t");
poem.append(thirdline);
poem.append( "\n\t");
poem.append(fourthline);
Last edited on
I see now, thanks very much! Program working!
Topic archived. No new replies allowed.