title: Save user input to a text file c++
body: <p>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.</p>
  
  <p>Here's what triggers the write-to-file:</p>
  
  <pre><code>else if(command == "write"){
     ofstream myfile (arg.c_str());
     string writeToFile;
     std::cout &lt;&lt; "Opening file '"&lt;&lt; arg.c_str() &lt;&lt; "'...\n" &lt;&lt; std::endl;
     std::cout &lt;&lt; "Plese enter what you want to write into the file:\n" &lt;&lt; std::end;
  
     std::getline(std::cin, writeToFile);
  
     if (myfile.is_open()){
       myfile &lt;&lt; writeToFile &lt;&lt; "\n";
       myfile.close();
     }
  
     std::cout &lt;&lt; "You wrote: " &lt;&lt; writeToFile &lt;&lt; std::endl;
     std::cout &lt;&lt; "File succesfully updated. \n" &lt;&lt; std::endl;
     commandStart();
   }
  </code></pre>
  
  <p>However when I use the <code>std::getline(std::cin, writeToFile);</code> this is the result:
  <a href="https://i.stack.imgur.com/Gk7iJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Gk7iJ.png" alt="Using std::getline(std::cin, writeToFile);"></a></p>
  
  <p>It doesn't let me type anything to save to the file and it automatically closes, however, when I use this:</p>
  
  <pre><code>else if(command == "write"){
     ofstream myfile (arg.c_str());
     string writeToFile;
     std::cout &lt;&lt; "Opening file '"&lt;&lt; arg.c_str() &lt;&lt; "'...\n" &lt;&lt; std::endl;
     std::cout &lt;&lt; "Plese enter what you want to write into the file:\n" &lt;&lt; std::end;
  
     std::cin &gt;&gt; writeToFile;
  
     if (myfile.is_open()){
       myfile &lt;&lt; writeToFile &lt;&lt; "\n";
       myfile.close();
     }
  
     std::cout &lt;&lt; "You wrote: " &lt;&lt; writeToFile &lt;&lt; std::endl;
     std::cout &lt;&lt; "File succesfully updated. \n" &lt;&lt; std::endl;
     commandStart();
   }
  </code></pre>
  
  <p>using the <code>std::cin &gt;&gt; writeToFile;</code> I'm allowed to type something and save it into the file, but it only saves the very first word:</p>
  
  <p><a href="https://i.stack.imgur.com/K4gaB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/K4gaB.png" alt="Using std::cin &gt;&gt; writeToFile;"></a></p>
  
  <p>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.</p>