-1

I've been writing a program that simulates the terminal on your computer. One of the options is to write some text and save it into an existing text file, however I've been having trouble saving the whole input into the file.

Here's what triggers the write-to-file:

else if(command == "write"){
   ofstream myfile (arg.c_str());
   string writeToFile;
   std::cout << "Opening file '"<< arg.c_str() << "'...\n" << std::endl;
   std::cout << "Plese enter what you want to write into the file:\n" << std::end;

   std::getline(std::cin, writeToFile);

   if (myfile.is_open()){
     myfile << writeToFile << "\n";
     myfile.close();
   }

   std::cout << "You wrote: " << writeToFile << std::endl;
   std::cout << "File succesfully updated. \n" << std::endl;
   commandStart();
 }

However when I use the std::getline(std::cin, writeToFile); this is the result: Using std::getline(std::cin, writeToFile);

It doesn't let me type anything to save to the file and it automatically closes, however, when I use this:

else if(command == "write"){
   ofstream myfile (arg.c_str());
   string writeToFile;
   std::cout << "Opening file '"<< arg.c_str() << "'...\n" << std::endl;
   std::cout << "Plese enter what you want to write into the file:\n" << std::end;

   std::cin >> writeToFile;

   if (myfile.is_open()){
     myfile << writeToFile << "\n";
     myfile.close();
   }

   std::cout << "You wrote: " << writeToFile << std::endl;
   std::cout << "File succesfully updated. \n" << std::endl;
   commandStart();
 }

using the std::cin >> writeToFile; I'm allowed to type something and save it into the file, but it only saves the very first word:

Using std::cin >> writeToFile;

Any ideas of why this is happening? I've already checked other questions and sites but I haven't been able to solve this issue.

Bruno Recillas
  • 861
  • 1
  • 9
  • 18
  • 3
    Please post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Possible duplicate: [c++ - Why does std::getline() skip input after a formatted extraction?](http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – MikeCAT Mar 11 '16 at 02:29
  • What's `copy` actually? – πάντα ῥεῖ Mar 11 '16 at 02:29
  • @πάνταῥεῖ sorry it was a variable I used to copy the 'writeToFile' but forgot to remove when posting. I already fixed in in the code – Bruno Recillas Mar 11 '16 at 02:31
  • 1
    basically the cout '\n' is read by your getline function. std::cin.ignore() after the cout() will solve your issue but you should read the answer in the linked duplicate to fully understand the cause – softwarenewbie7331 Mar 11 '16 at 03:18
  • @softwarenewbie7331 yes I read it. I didn't find that post before. Thanks for the help. If you write down what you said as an answer I'll mark it as selected answer – Bruno Recillas Mar 11 '16 at 04:43

1 Answers1

0

basically the cout '\n' is read by your getline function. std::cin.ignore() after the cout() will solve your issue.

as mentioned by MikeCAT Why does std::getline() skip input after a formatted extraction? answers go in depth to explain the reason, definitely worth a read.

Community
  • 1
  • 1