anyone please help

I am trying to write a program but i have ran into a problem i need to save a sentence and output it into a text file and i can not seem to get it to work can someone please tell me what to do and give me a example?
i know this is wrong but these is what i want to do

char date;
out << "Enter date" <<endl;
cin >> date;

ofstream myfile;
myfile.open ("date.txt");
myfile << date
myfile.close();

but it always comes out the first word even when i try doing strings someone help?
Cin just gets the first word
Look up getline
Last edited on
see if this helps

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
#include <iostream>
#include <fstream>
using namespace std;

char date[12];

int main(){
	char filename[81];

	cout << "Enter a file name and  press ENTER: ";
	cin.getline(filename, 80);
	ofstream file_out(filename);
	if (!file_out) {
		cout << "File " << filename;
		cout << "could not be opened. ";
		return -1;
	}
	cout << "Enter a date: ";
	cin.getline (date,11);
	cout << "File " << filename << " was opened. ";
	file_out << date << endl;
	file_out << "put some text hear" << endl;
	file_out << "and some more text hear " << endl;
	file_out.close();
	return 0;
}
Topic archived. No new replies allowed.