I'm getting an error while trying to use a user defined variable, as the filename for myfile.open (variable) - however when I change the variable to a static filename in quotes "filename.txt" it works.
if (responseMenu == 'a') {
// Create user .txt file and populate with user input
string userFilename;
cout << "Please enter a filename for your .txt document." << endl;
cin >> userFilename;
cout << endl;
cout << endl;
// Create file and accept user input
ofstream myfile;
myfile.open (userFilename);
string input;
cout << "Please enter the content to be saved into file: " << endl;
cout << "To save to file and exit, enter semicolon(;)" << endl;
cout << "This program can save multiple new lines." << endl;
cout << " " << endl;
while (input != ";") {
getline (cin, input);
myfile << input << "\n";
}
myfile.close();
bool saveToFile( std::string &in )
{
//Create a sdt::string for user input, for the filename.
std::string fileName = "";
//Output a message and then get user input.
std::cout << "\n\n\tPlease enter a filename to save to:\n\tFilename: ";
std::getline( std::cin, fileName, '\n' );
//Create an outfile to save to.
std::ofstream oFile;
oFile.open( fileName + ".txt", std::ios::out );
if( oFile.is_open() )
{
//code...
returntrue;
}
else
std::cout << "\n\tSaving to file failure.\n\n";
returnfalse;
}