User input string to text file

Hey guys.
I'm trying to make a program that reads a string that is entered by a user, and outputs that string to a text file. Everytime I run this code, the text stored in response doesn't get put into the example.txt file that the program creates.
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
#include <fstream>
#include <cstdlib>
#include <iostream>

using namespace std;

int main() {

	string response;
	
	cout<<"Please enter some text" << endl;
	getline(cin, response);

	ofstream myFile;
	myFile.open("example.txt");
	cout<<"Response is " << response <<endl;
	myFile <<response;
	myFile.close();

	cout<<"Done..." <<endl;

	system("PAUSE");
	return 0;

}


If I just run the code like this, and enter the string right on there, it works.
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 <fstream>
#include <cstdlib>
#include <iostream>

using namespace std;

int main() {

//      Putting response in place of the text also works in this situation.
	string response = "spaghetti";

//	cout<<"Please enter some text";
//	getline(cin, response);
//	cout<<"Copying text to file..."<< endl;

	ofstream myFile;
	myFile.open("example.txt");
//	cout<<"Response is " << response <<endl;
	myFile <<"SPAGHETTI\n";
	myFile.close();

	cout<<"Done..." <<endl;

	system("PAUSE");
	return 0;

}


I have no clue what I am doing wrong. I don't know if it's the IDE I'm using (eclipse), or if I am messing something up somewhere. Can anyone help me out?
Last edited on
Topic archived. No new replies allowed.