title: How to prevent pieces of colored text in the command line turning back white using std::wcout?
body: <p>I recently got back into C++ and this is just beyond me. This cmd app, when compiled using the debugger in VS2022, looks perfectly as it should. However, when I run the compiled project executable, a seemingly random piece of text often <a href="https://i.stack.imgur.com/dtQxu.png" rel="nofollow noreferrer">changes its color to the default white.</a></p>
  <p>It's obviously not noticeable when it happens with white text, but I intend to use SetConsoleTextAttribute (or any other way to print colored text) in my program, as well as Unicode support, which _getline does just fine.</p>
  <p>It appears to be caused by wcout, as removing _setline and changing them back to the narrow type fixed the issue. However, just removing _setline doesn't do anything.</p>
  <p>Code:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;Windows.h&gt;
  #include &lt;io.h&gt;
  
  using namespace std;
  
  int main() {
      
      HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  
      fflush(stdout);
      (void)_setmode(_fileno(stdout), _O_U16TEXT);
  
      SetConsoleTextAttribute(hConsole, 10);
      
      wcout &lt;&lt; L&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit,\n&quot;;
      wcout &lt;&lt; L&quot;sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n&quot;;
      
      SetConsoleTextAttribute(hConsole, 11);
  
      wcout &lt;&lt; L&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit,\n&quot;;
      wcout &lt;&lt; L&quot;sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n&quot;;
  
      SetConsoleTextAttribute(hConsole, 12);
  
      wcout &lt;&lt; L&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit,\n&quot;;
      wcout &lt;&lt; L&quot;sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n&quot;;
  
      cin.get();
  }
  </code></pre>
  <p>EDIT: Using wmain instead of main and calling fflush wherever it made the tiniest bit of sense didn't fix anything either. Weird.</p>