Everything seems to work when main() runs newFile() except for the fact that it only writes everything after the first word. So if I input: this is a test file. It will write the text to the file as " is a test file."
Here is my code with your solution. Still the same results.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
void newFile()
{
std::cout << "New File Name: ";
std::cin >> gFileName;
std::cout << "Save To: ";
std::cin >> gFileLoc;
std::ofstream file_(gFileName, std::ofstream::app);
if (file_.is_open())
{
std::cout << "File saved in '" << gFileLoc << "'. File ready for writing. Press ENTER when finished." << std::endl;
std::cin >> gFileText;
std::getline(std::cin, gFileText);
file_ << gFileText;
}
}
So basically at line 14 you enter the text you want inside the file. For example if you type "Cplusplus is a great resource" it WILL write the file, but it will cut the first word off and read " is a great resource" inside the file.
Ah, okay sorry I midunderstod your problem, delete that cin
Ithink the reason is the first word read by cin after meet space cin will stop to read, rest of the sentence will be read by getline
looks like just adding std::cin.ignore(); with no parameters was enough. Not sure what the whole 1024 thing was you mentioned but with no parameters, it seems to work.