I am trying to create a program that allows the user to do 2 things (create a new file in the current directory, or search a file and display the word count) The word count works fine but when I try to allow the user to create a file it starts going wrong. I first ask the user to input a filename, and then enter some initial data into a string variable. That string variable gets passed to the createFile() function which takes care of entering the data into the file.
When I run this program I enter the file data I wish to put in the created file, say for example " Hello File Data" and when I look into that file once its been created there is only the first word ("Hello") in the case of this example. Here is the code:
cout << "Enter the file name: " << endl;
cin >> filename;
outFile.open(filename.c_str());//create the file
if (!outFile)
{
//file could not be created
cout<< "File could not be completed" << endl;
break;
}
else // file was created
cout << "FILE WAS COMPLETED NAMED: " << filename << endl;;
//ask the user to put some data y/n
cout << "Would you like to set some initial data in the file? y/n" << endl;
cin >> choice;
switch(choice)
{
case'y':
cout << "Enter the data: " << endl;
cin >> filedata;
user1.createFile(outFile, filedata);
break;
case'n':
cout << "Ok then dont create initial data... Your created file is blank" << endl;
break;
}
The create filefunction:
1 2 3 4 5 6 7 8 9
void User::createFile(ofstream &outfile, string filedata)
{
//output the file into outfile
outfile << filedata;
cout << "You have successfully saved the data you entered" << endl;
}
thanks, but when I use getline() it doesn't let me enter data... it just skips to say you have entered the data successfully and just puts nothing in the file